我正在尝试将 NSData 对象(可以是图像或视频)上传到 Amazon S3。
当文件大小低于 5Mb 时,一切正常。当它更大时,AWS SDK 将切换到分段上传,然后失败并显示:
匿名用户无法启动分段上传。请认证。
我正在使用当前可用的最新 AWSS3 开发工具包版本 (2.5.5)。
- (void) uploadDataToAWS: (NSData *) mediaData
contentType: (NSString *) contentType
bucket: (NSString *) bucket
region: (NSString *) region
objectKey: (NSString *) objectKey
accessKeyId: (NSString *) accessKeyId
secretAccessKey: (NSString *) secretAccessKey
sessionToken: (NSString *) sessionToken
progressHandler: (void (^) (int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend)) progressHandler
withCompletionHandler: (void (^) (BOOL success)) completionHandler {
OwnAWSCredentialsProvider *credentialsProvider = [OwnAWSCredentialsProvider credentialsWithAccessKey:accessKeyId secretKey:secretAccessKey sessionKey:sessionToken];
AWSServiceConfiguration *awsServiceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1
credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = awsServiceConfiguration;
//Since S3 does not support objects uploads (only files), will write first locally:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.forUpload", [self randomStringWithLength:10]]];
[mediaData writeToFile:filePath atomically:YES];
//Configure upload request
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = bucket;
uploadRequest.key = objectKey;
uploadRequest.body = [NSURL fileURLWithPath:filePath];
uploadRequest.contentType = contentType;
uploadRequest.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend){
progressHandler (bytesSent, totalBytesSent, totalBytesExpectedToSend);
};
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
[[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) {
//Remove the generated file
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
if (task.error != nil) {
if( task.error.code != AWSS3TransferManagerErrorCancelled
&& task.error.code != AWSS3TransferManagerErrorPaused) {
NSLog(@"(%i) %@", (int)task.error.code, [task.error localizedDescription]);
NSLog(@"Message: %@", task.error.userInfo[@"Message"]);
}
completionHandler (NO);
} else {
completionHandler(YES);
}
return task;
}];
}
拥有AWSCredentialsProvider.m:
#import "OwnAWSCredentialsProvider.h"
@implementation OwnAWSCredentialsProvider
+ (instancetype)credentialsWithAccessKey:(NSString *)accessKey
secretKey:(NSString *)secretKey
sessionKey:(NSString*)sessionKey
{
OwnAWSCredentialsProvider *credentials = [[OwnAWSCredentialsProvider alloc] initWithAccessKey:accessKey secretKey:secretKey sessionKey:sessionKey];
return credentials;
}
- (instancetype)initWithAccessKey:(NSString *)accessKey
secretKey:(NSString *)secretKey
sessionKey:(NSString*)sessionKey
{
if (self = [super init]) {
_accessKey = accessKey;
_secretKey = secretKey;
_sessionKey = sessionKey;
}
return self;
}
- (AWSTask<AWSCredentials *> *)credentials {
return [[AWSTask taskWithResult:nil] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) {
return task;
}];
}
- (void)invalidateCachedTemporaryCredentials {
}
@end
每次上传都会生成凭据,它们是有效的,并且在小于 5Mb 的文件上工作得很好。
我不使用预签名的 URL,所以我需要让这段代码以某种方式工作。
任何帮助表示赞赏。