Ali OSS 云存储使用

OSS About 5,241 words

注意

OSSJavasdk 3.13.2中的分片上传合并接口completeMultipartUpload,构建CompleteMultipartUploadRequest对象时partETags参数不能为空,否则会抛出空指针异常。

解决方法:升级到3.15.2版本。

Object命名规则:使用UTF-8编码,长度必须在1~1023字符之间,不能以正斜线(/)或者反斜线(\)字符开头。

普通上传:最大5G,简单上传默认会覆盖同名Object

分片上传:最小分片100K,最多10000个分片。

构建 OSS 客户端

使用域名构建:

public OSS getOssClient(String endpoint, String accessKeyId, String accessKeySecret) {
    return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
}

使用STSAssumeRole构建:

public OSS getOssClientUseSTS(String region, String accessKeyId, String accessKeySecret, String roleArn) {
    try {
        STSAssumeRoleSessionCredentialsProvider provider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(
            regionId, accessKeyId(), accessKeySecret(), roleArn);
        return new OSSClientBuilder().build(bucketStoreConfig.getEndpoint(), provider);
    } catch (ClientException e) {
        log.error("build oss client exception#{}", e.getMessage(), e);
        throw new RuntimeException(e);
    }
}

文档:https://help.aliyun.com/document_detail/32010.html

直接上传

public String uploadFile(String bucketName, String objectName, InputStream is) {
    OSS ossClient = null;
    try {
        ossClient = getOssClient();
        ossClient.putObject(bucketName, objectName, is);
        return objectName;
    } catch (Exception e) {
        throw e;
    } finally {
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }
}

下载文件

public void downloadToFile(String bucketName, String objectName, String pathName) {
    OSS ossClient = null;
    try {
        ossClient = getOssClient();
        GetObjectRequest req = new GetObjectRequest(bucketName, objectName);
        ossClient.getObject(req, new File(pathName));
    } catch (Exception e) {
        throw e;
    } finally {
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }
}

分片上传

初始化

public String initMultipartUpload(String bucketName, String objectName) {
    OSS ossClient = null;
    try {
        ossClient = getOssClient();
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName);
        InitiateMultipartUploadResult result = ossClient.initiateMultipartUpload(request);
        return result.getUploadId();
    } catch (Exception e) {
        throw e;
    } finally {
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }
}

上传分片

public void uploadPart(int partNumber, String bucketName, String objectName, InputStream is, String uploadId) {
    OSS ossClient = null;
    try {
        ossClient = getOssClient();
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(objectName);
        uploadPartRequest.setUploadId(uploadId);
        uploadPartRequest.setInputStream(is);
        uploadPartRequest.setPartNumber(partNumber);
        ossClient.uploadPart(uploadPartRequest);
    } catch (Exception e) {
        throw e;
    } finally {
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }
}

合并分片

public void completeMultipartUpload(String bucketName, String objectName, String uploadId) {
    OSS ossClient = null;
    try {
        ossClient = getOssClient();
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, null);
        Map<String, String> headers = new HashMap<>();
        headers.put("x-oss-complete-all", "yes");
        completeMultipartUploadRequest.setHeaders(headers);
        ossClient.completeMultipartUpload(completeMultipartUploadRequest);
    } catch (Exception e) {
        throw e;
    } finally {
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }
}

终止上传

public void abortMultipartUpload(String bucketName, String objectName, String uploadId) {
    OSS ossClient = null;
    try {
        ossClient = getOssClient();
        AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, objectName, uploadId);
        ossClient.abortMultipartUpload(abortMultipartUploadRequest);
    } catch (Exception e) {
        throw e;
    } finally {
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }
}

签名(防盗链)

public String signUrl(String bucketName, String objectName, String process, Integer expires) {
    OSS ossClient = null;
    try {
        ossClient = getOssClient();
        GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
        if (StringUtils.hasText(process)) {
            req.setProcess(process);
        }
        Duration expireTime = Duration.ofSeconds(expires);
        LocalDateTime expire = LocalDateTime.now().plus(expireTime);
        Date expireDate = Date.from(expire.atZone(ZoneId.systemDefault()).toInstant());
        req.setExpiration(expireDate);
        URL url = ossClient.generatePresignedUrl(req);
        return url.toString();
    } finally {
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }
}

文档

https://help.aliyun.com/document_detail/32011.html

Views: 962 · Posted: 2023-04-04

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh