Reputation: 22698
I am trying to upload a NSData object (which can be either an image or a video) to Amazon S3.
Everything works good when the file size is lower than 5Mb. When it is bigger, the AWS SDK is switching to multipart upload and then it fails with:
Anonymous users cannot initiate multipart uploads. Please authenticate.
I am using the latest AWSS3 SDK version currently available (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;
}];
}
OwnAWSCredentialsProvider.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
Credentials are generated on each upload and they are valid and working great on files smaller than 5Mb.
I don't use pre-signed URLs, so I need to make this piece of code to work somehow.
Any help is appreciated.
Upvotes: 0
Views: 525
Reputation: 22698
For anyone having the same issue, I have finally found where was the problem:
I used OwnAWSCredentialsProvider, as a wrapper of AWSCredentialsProvider.
Here, the getter for credentials was always returning nil (not sure why it worked for small files).
Instead, I am using now AWSBasicSessionCredentialsProvider, provided by the AWS SDK and everything works good.
AWSBasicSessionCredentialsProvider *credentialsProvider = [[AWSBasicSessionCredentialsProvider alloc] initWithAccessKey:accessKeyId secretKey:secretAccessKey sessionToken:sessionToken];
Upvotes: 1