OSS文件上傳/下載

1、文件上傳

public ReturnMessage<Map<String,Object>> uploadFile(String ossFileKey,File file ) throws Exception {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            PutObjectResult putObjectResult = ossClient.putObject(new PutObjectRequest(bucketName, ossFileKey, file));
            if(putObjectResult!=null){
                // 設置URL過期時間爲1小時。
        	Date expiration = new Date(System.currentTimeMillis() + OssConstants.URL_EXPIRATION_TIME);
        	//生成以GET方法訪問的簽名URL,訪客可以直接通過瀏覽器訪問相關內容。
        	URL url = ossClient.generatePresignedUrl(bucketName, ossFileKey, expiration);
                Map<String,Object> rtnValue = new HashMap<>();
                rtnValue.put("url",url.toString());
                rtnValue.put("ossFileKey", ossFileKey);
                rtnValue.put("name",file.getName());
                return  new ReturnMessage<>(true,"" ,rtnValue);
            }else{
                return  new ReturnMessage<>(false,ResultStatusConstants.SYSTEM_ERROR);
            }
        } catch (OSSException oe) {
        		log.error("Error Message :  " + oe.getMessage() ,oe);
            throw oe;
        } catch (ClientException ce) {
            log.error("Error Message :  " + ce.getMessage() ,ce);
            throw ce;
        } finally {
            ossClient.shutdown();
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章