S3是Simple Storage Service的缩写,即简单存储服务。亚马逊的名词缩写也都遵循这个习惯,例如Elastic Compute Cloud缩写为EC2等等。
目前腾讯云很多对象存储组件都是兼容s3协议的,因此我们实现标准的s3协议存储调用即可在各类对象存储场景中使用,show me the code
初始化s3 认证
AmazonS3 initAmazonS3Object() {
// 新建一个凭证
AWSCredentials credentials = new BasicAWSCredentials(("ak","sk");
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.withProtocol(Protocol.HTTPS);
AmazonS3 amazonS3Client = new AmazonS3Client(credentials, clientConfig);
// 对象网关地址
amazonS3Client.setEndpoint(properties.getEndpoint());
return amazonS3Client;
}
上传的具体方法
StoreEntity uploadFile(MultipartFile file) throws UniStorageException {
Assert.notNull(file, UniStorageProperties.PARAM_MUST_NOT_EMPTY);
String originalName = file.getOriginalFilename();
assert originalName != null;
String fileType = originalName.substring(originalName.lastIndexOf(".") + 1);
String storageName = IdGen.uuid() + "." + fileType;
//路径修改
String filePath = UniStorageUtils.timePathYMD(System.currentTimeMillis()) + "/" + storageName;
System.setProperty("com.amazonaws.sdk.disableCertChecking", "true");
AmazonS3 amazonS3 = initAmazonS3Object();
try {
//文件名:filePath, 文件流:file.getInputStream()
amazonS3.putObject(properties.getBucketName(), filePath, file.getInputStream(), new ObjectMetadata());
//文件名:filePath,CannedAccessControlList.Private:私有
amazonS3.setObjectAcl(properties.getBucketName(), filePath, CannedAccessControlList.Private);
} catch (Exception e) {
log.error("存储异常{}", e);
throw new Exception("存储异常");
}
return new StoreEntity(storageName, originalName, filePath, file.getSize(), fileType);
}
以上代码在ceph、minio测试通过
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有