我正在尝试在iOS上执行AWS上传。我循环遍历一组AWSS3TransferManagerUploadRequest
个对象,并将大量不同大小的图像(1000+)上传到S3存储桶。使用:
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
[[transferManager upload:request] continueWithExecutor:[BFExecutor mainThreadExecutor]
withBlock:^id(BFTask *task) {
一旦我了解了第700张图片,上传进度开始变得非常慢,然后我开始在其他所有后续图片上传时收到这些错误:
2015-03-27 11:07:30.238 crafted[4979:1414229] Error: Error Domain=com.amazonaws.AWSGeneralErrorDomain Code=3 "The operation couldn’t be completed. (com.amazonaws.AWSGeneralErrorDomain error 3.)" UserInfo=0x1709b5b60 {SignatureProvided=f8c21dd55c323378b05c660a5f27255a2aecbd6eee1f2ca0c5fb0460136dde30, RequestId=DB37767F9C1AFD7E, StringToSign=AWS4-HMAC-SHA256-PAYLOAD
20150327T160658Z
20150327/us-west-2/s3/aws4_request
1e63d8ac434ee08f333a4f59c52fc3565e5b451cca9c7f1a946e209fb72f24c5
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
11b7db18ee4cbdedc46907daa5e99688b2be72da3a943ac0354a9405b7c59f19, Code=SignatureDoesNotMatch, HostId=0V4bjdhjCG/wt/NAlsgr1K7DV6QkUdYOlwjFRfmBI/swFesufXVi19PTgacwOBt1, StringToSignBytes=41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 2d 50 41 59 4c 4f 41 44 0a 32 30 31 35 30 33 32 37 54 31 36 30 36 35 38 5a 0a 32 30 31 35 30 33 32 37 2f 75 73 2d 77 65 73 74 2d 32 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 31 65 36 33 64 38 61 63 34 33 34 65 65 30 38 66 33 33 33 61 34 66 35 39 63 35 32 66 63 33 35 36 35 65 35 62 34 35 31 63 63 61 39 63 37 66 31 61 39 34 36 65 32 30 39 66 62 37 32 66 32 34 63 35 0a 65 33 62 30 63 34 34 32 39 38 66 63 31 63 31 34 39 61 66 62 66 34 63 38 39 39 36 66 62 39 32 34 32 37 61 65 34 31 65 34 36 34 39 62 39 33 34 63 61 34 39 35 39 39 31 62 37 38 35 32 62 38 35 35 0a 31 31 62 37 64 62 31 38 65 65 34 63 62 64 65 64 63 34 36 39 30 37 64 61 61 35 65 39 39 36 38 38 62 32 62 65 37 32 64 61 33 61 39 34 33 61 63 30 33 35 34 61 39 34 30 35 62 37 63 35 39 66 31 39,
AWSAccessKeyId=ASIAJCSIGUTUKG2HSYCA,
Message=The request signature we calculated does not match the signature you provided. Check your key and signing method.}
“我们计算的请求签名与您提供的签名不符。请检查您的密钥和签名方法。”
我不知道为什么会这样,我无法在这个问题上找到类似的问答。
编辑: 我正在使用Cognito Identity pool进行身份验证,并仅为put提供对s3存储桶的匿名访问。
编辑2:Code Per Yosuke的请求(我只是构建一个请求对象数组,然后循环遍历数组并上传每个请求):
- (void)performS3UploadWithRequest:(NSMutableArray *)requests
{
for (AWSS3TransferManagerUploadRequest *request in requests) {
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
__weak typeof(self) weakSelf = self;
[[transferManager upload:request] continueWithExecutor:[BFExecutor mainThreadExecutor]
withBlock:^id(BFTask *task) {
if (task.error) {
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
switch (task.error.code) {
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:
break;
default:
NSLog(@"Error: %@", task.error);
break;
}
} else {
// Unknown error.
NSLog(@"Error: %@", task.error);
}
}
if (task.result) {
AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
// The file uploaded successfully.
NSLog(@"%@", uploadOutput);
[weakSelf countUploadProgress];
}
return nil;
}];
}
}
- (AWSS3TransferManagerUploadRequest *)createUploadRequestWithDestinationBucket:(NSString *)destBucket Key:(NSString *)key Body:(NSURL *)body
{
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = destBucket;
uploadRequest.key = key;
uploadRequest.body = body;
uploadRequest.contentType = @"binary/octet-stream";
uploadRequest.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
dispatch_async(dispatch_get_main_queue(), ^{
if (totalBytesExpectedToSend > 0) {
NSLog(@"%f", (float)((double) totalBytesSent / totalBytesExpectedToSend));
}
});
};
return uploadRequest;
}
答案 0 :(得分:1)
感谢Yosuke,我找到了一个solution,它按顺序运行for循环。它有点慢,但没有任何问题,即使有1000多张照片上传,仍然很快。
- (void)performS3UploadWithRequest:(NSMutableArray *)requests
{
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
BFTask *task = [BFTask taskWithResult:nil];
__weak typeof(self) weakSelf = self;
for (AWSS3TransferManagerUploadRequest *request in requests) {
task = [task continueWithSuccessBlock:^id(BFTask *task) {
return [[transferManager upload:request] continueWithExecutor:[BFExecutor mainThreadExecutor]
withBlock:^id(BFTask *task) {
if (task.error) {
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
switch (task.error.code) {
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:
break;
default:
NSLog(@"Error: %@", task.error);
break;
}
} else {
// Unknown error.
NSLog(@"Error: %@", task.error);
}
}
if (task.result) {
AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
[weakSelf countUploadProgress];
// The file uploaded successfully.
NSLog(@"%@", uploadOutput);
}
return nil;
}];
}];
[task continueWithSuccessBlock:^id(BFTask *task) {
return nil;
}];
}
}