S3 client向ceph上传文件注意事项
创始人
2024-04-07 13:24:14
0

背景:

众所知周,AWS S3 client功能强大,能与各类符合OSS标准的存储进行工作。本项目中刚好用到ceph,就是直接使用S3 java client上传文件。在根据ceph官网的的一些文档操作时遇到一些问题,这里记录下来,仅供遇到类似问题的网友参考。

问题与解决方法

按照ceph https://docs.ceph.com/en/quincy/radosgw/s3/java/ 官网的示例,在本人环境中无法遍历bucket或者upload文件, 在多方搜索对比测试后发现,需要添加几个参数才行,目前不确定是否是因为本地ceph的设置导致的问题, 这里记录下来仅供参考。

 //  disable SSL cert checkString accessKey = "insert your access key here!";String secretKey = "insert your secret key here!";//具体的域名, 每个项目或者公司都不一样String cephHost = "http://cepha.b.com";System.setProperty(SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY,"true");if (SDKGlobalConfiguration.isCertCheckingDisabled()){System.out.println("Cert checking is disabled");}// S3 Client configurationClientConfiguration config = new ClientConfiguration();// Not the standard "AWS3SignerType", maar expliciet signerTypeV2config.setSignerOverride("S3SignerType");// or else Amazon S3; Status Code: 403; Error Code: SignatureDoesNotMatch will happen. 这个也很重要,不然给putRequest 添加一些mata时会报SignatureDoesNotMatch的错误config.setSignerOverride("AWSS3V4SignerType");config.setProtocol(Protocol.HTTP);BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(cephHost, "local-ceph");
// 重点就是这个 "local-ceph", 不设置它,在本人项目中,可以list buckt但是不能upload fileAmazonS3 s3Client = AmazonS3ClientBuilder.standard().withClientConfiguration(config).withCredentials(new AWSStaticCredentialsProvider(credentials)).withEndpointConfiguration(endpointConfiguration).enablePathStyleAccess().disableChunkedEncoding().build();

对比一下ceph官网的文档

String accessKey = "insert your access key here!";
String secretKey = "insert your secret key here!";AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonS3 s3Client = new AmazonS3Client(credentials);
s3Client.setEndpoint("objects.dreamhost.com");

其他

在创建了AmazonS3 对象后记可以操作bucket中内容

 List buckets = s3Client.listBuckets();

上传文件,并带有meta信息,

try {ObjectMetadata metaData = new ObjectMetadata();Map userMetaData = new HashMap<>();userMetaData.put("key1", "val1");userMetaData.put("key2", "val2");userMetaData.put("key3", "val3");metaData.setUserMetadata(userMetaData);PutObjectRequest putObjReq = new PutObjectRequest(bucketName, fileKey, file);putObjReq.setMetadata(metaData);PutObjectResult result =  s3Client.putObject(putObjReq);log.info("upload result {}", result);} catch (Exception ex) {log.error("fail to  upload to fileKey {}, exMsg:{}",fileKey, ex.getMessage(), ex);}

如果上传的文件较大,使用TransferManager, 参看S3官方文档
https://docs.aws.amazon.com/zh_cn/sdk-for-java/v1/developer-guide/examples-s3-transfermanager.html

      PutObjectRequest putObjReq = new PutObjectRequest(bucketName, fileKey, file);putObjReq.setMetadata(metaData);TransferManager tm = TransferManagerBuilder.standard().withS3Client(s3Client).build();Upload upload = tm.upload(putObjReq, progressListener);log.info("start to upload file '{}' to fileKey:{},",file.getAbsolutePath(), fileKey);// wait for the upload to finish before continuing.upload.waitForCompletion();

相关内容

热门资讯

“搬”进校园后 形成医校合作新... 转自:沈阳晚报  从牙齿发育到脊柱形态,从体重管理到视力保护……今年4月以来,沈阳市儿童医院联合皇姑...
特朗普,“赢”四票 据新华社报道,美国国会众议院3日下午以218票赞成、214票反对的表决结果通过了总统特朗普推动的“大...
分析师称中国已购买多达70万吨... 来源:饲料行业信息网  分析师表示,中国公司已签约购买多达70万吨2025年乌克兰大麦。强劲需求将本...
点赞致敬!巷道里的中国脊梁找到... 【点赞致敬!#巷道里的中国脊梁找到了#】#泥泞中奋力清淤的子弟兵找到了#近日,贵州榕江,一个在泥泞中...
原创歌曲《我们来自森林大草原》...   穿过人山人海  走上这番舞台  在6月28日播出的  星光大道舞台上  来自  内蒙古森林消防总...
投资者提问:氧化锆瓷块(核心原... 投资者提问:氧化锆瓷块(核心原料)的 自给率 多少?若日本东曹等供应商断供,是否已与国瓷材料等国内厂...
美越贸易协议令人宽慰 但细节寥... 格隆汇7月4日|美国与越南达成的贸易协议对这个东南亚国家乃至更广泛的地区而言似乎是利好。但分析人士称...
史钦琛:指挥若定 血战捐躯 转自:邯郸日报 史钦琛(1913—1940),原名史序书,出生在成安县亦村的一个富裕农民家庭。193...
为一线职工“送清凉” 转自:邯郸日报 本报讯(记者王英)为进一步做好高温天气作业劳动保护工作,有效防控中暑事件和生产安全事...
往事就在昨天   最近翻开相册,一张张老照片呈现在眼前。  照片记录了40年前邮电局的那段岁月,有当时人们选择电报...