配置信息:
- SDK
AWSS3: 2.33.4
- Language:
OC
全部代码如下:
本机搭建的minio服务
AWSDDLog.sharedInstance.logLevel = AWSDDLogLevelVerbose;
[AWSDDLog addLogger:AWSDDTTYLogger.sharedInstance];
AWSStaticCredentialsProvider *provider = [[AWSStaticCredentialsProvider alloc]
initWithAccessKey:@"WLZO9XjvNdBjsbKppE36" secretKey:@"FpFIhYUoxpGGTUiSrHMUl78ugSuMQ5k2ZscFht4W"];
AWSEndpoint *minioEndpoint = [[AWSEndpoint alloc] initWithRegion:AWSRegionUSWest2 service:AWSServiceS3 URL:
[NSURL URLWithString:@"http://192.168.65.202:9000"]];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2
endpoint:minioEndpoint credentialsProvider:provider localTestingEnabled:true];
configuration.maxRetryCount = 1;
configuration.timeoutIntervalForRequest = 10;
configuration.timeoutIntervalForResource = 10;
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
[AWSS3 registerS3WithConfiguration:configuration forKey:@"USWest2S3"];
NSMutableString *dataString = [NSMutableString new];
for (int32_t i = 1; i < 10000000; i++) {
[dataString appendFormat:@"%d\n", i];
}
NSData *testData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
AWSS3 *S3 = [AWSS3 S3ForKey:@"USWest2S3"];
NSString *fileName= @"ios_test.txt";
AWSS3PutObjectRequest *request = [[AWSS3PutObjectRequest alloc] init];
request.bucket = @"ios.bucket.test";
request.key = fileName;
request.contentType = @"text/plain";
request.ACL = AWSS3ObjectCannedACLPublicRead;
request.body = testData;
request.contentLength = @(testData.length);
[S3 putObject:request completionHandler:^(AWSS3PutObjectOutput * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSLog(@"succeed");
}else{
NSLog(@"failed: %@", error.localizedDescription);
}
}];
问题原因:
主要是bucket name的格式问题会导致上传失败,比如ios-test-bucket,这种-符号链接起来就不能上传到minio,这个bucket name会作为主机名拼在域名的前面,所以导致域名访问不了;
解决办法:如果能改bucket name的格式就改(判断规则在函数aws_isVirtualHostedStyleCompliant
里面);
不能改就只能去改代码了;主要是改函数updateRequestToUseVirtualHostURL
里面的aws_isVirtualHostedStyleCompliant的判断;我们公司后端了为了跟阿里云统一所以就只能改代码了;