繁体   English   中英

AWSS3GetPreSignedURLRequest上传,自定义标题为403

[英]AWSS3GetPreSignedURLRequest upload with custom headers giving 403

我的AWSS3GetPreSignedURLRequest和AFNetworking的NSMutableURLRequest遇到了一些问题。 我可以使用Content-Type标头成功上传文件。 但是,如果我添加x-amz-aclx-amz-server-side-encryption ,则上传将失败,并显示403 - No Permission错误。 我能做什么? 这是亚马逊方面的问题,使用服务器端加密或ACL不允许使用预先签名的URL,或者更改请求是否有效? 我对AWS文档和iOS SDK参考资料非常深入,但没有任何内容。 顺便说一句,我正在使用AWS iOS SDK v2。 有人怎么怎么做?

    NSString *keyName;
    NSString *fileContentTypeStr;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM.d.h.mm.ss"];
    if([multipleFileType isEqualToString:@"PNG"])
    {
        fileContentTypeStr = @"image/png";
        keyName = [NSString stringWithFormat:@"image_%@.png", [formatter stringFromDate:[NSDate date]]];
    }
    else if([multipleFileType isEqualToString:@"JPG"])
    {
        fileContentTypeStr = @"image/jpeg";
        keyName = [NSString stringWithFormat:@"image_%@.jpg", [formatter stringFromDate:[NSDate date]]];
    }
    self.imageUploadURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"image"]];

    [imageData writeToURL:self.imageUploadURL atomically:YES];
    AWSS3GetPreSignedURLRequest *getPreSignedURLRequest = [AWSS3GetPreSignedURLRequest new];
    getPreSignedURLRequest.bucket = [NSString stringWithFormat:@"BUCKET-NAME/%@",  folderObject.objectId];
    getPreSignedURLRequest.key = keyName;
    getPreSignedURLRequest.HTTPMethod = AWSHTTPMethodPUT;
    getPreSignedURLRequest.expires = [NSDate dateWithTimeIntervalSinceNow:3600];
    getPreSignedURLRequest.contentType = fileContentTypeStr;

    [[[AWSS3PreSignedURLBuilder defaultS3PreSignedURLBuilder] getPreSignedURL:getPreSignedURLRequest]
     continueWithBlock:^id(BFTask *task) {
         if (task.error) {
             NSLog(@"Error: %@",task.error);
         } else {
             NSURL *presignedURL = task.result;
             NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:presignedURL];
             [URLRequest setValue:fileContentTypeStr forHTTPHeaderField:@"Content-Type"];
             [URLRequest setValue:@"AES-256" forHTTPHeaderField:@"x-amz-server-side-encryption"];
             [URLRequest setValue:@"private" forHTTPHeaderField:@"x-amz-acl"];
             URLRequest.HTTPMethod = @"PUT";
             URLRequest.HTTPBody = imageData;

             AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
             NSProgress *progress;
             NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:URLRequest progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
             if(!error){
             NSLog(@"File was successfully uploaded.");
             }
             }];
             [uploadTask resume];
         }
         return nil;
     }];

注意:那里的代码示例缺少全局标题上的一些内容,还有一些其他完成块+委托,但我相信每个人都会明白这一点。

期待的感谢,

正如@YosukeMatsuda在评论中注意到的那样,您可以使用putObjectAcl:方法。

我把上传对象的ACL放在URLSession didCompleteWithError:委托方法上,如下所示:

//
self.awss3 = [[AWSS3 alloc] initWithConfiguration:__your_config__];
//

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    if (error)
    {
        NSLog(@"S3 UploadTask: %@ completed with error: %@", task, [error localizedDescription]);
    }
    else
    {
//      AWSS3GetPreSignedURLRequest does not contain ACL property, so it has to be set after file was uploaded to bucket
        AWSS3PutObjectAclRequest *aclRequest = [AWSS3PutObjectAclRequest new];
        aclRequest.bucket = @"your_bucket";
        aclRequest.key = @"your_key";
        aclRequest.ACL = AWSS3ObjectCannedACLPublicRead;

        [[self.awss3 putObjectAcl:aclRequest] continueWithBlock:^id(BFTask *bftask) {
            if (bftask.error)
            {
                NSLog(@"Error putObjectAcl: %@", [bftask.error localizedDescription]);
            }
            else
            {
                AWSEndpoint *endpoint =  self.awss3.configuration.endpoint;
                NSURL *publicReadURL = [[endpoint.URL URLByAppendingPathComponent:backgroundUploadTask.bucket] URLByAppendingPathComponent:backgroundUploadTask.key];
            }
            return nil;
        }];
    }
}

我们添加了- setValue:forRequestParameter: to AWSS3GetPreSignedURLRequest 您可以使用此方法添加ACL和加密标头。

此外,我们还推出了新的AWSS3TransferUtility以简化后台传输。 有关更多详细信息,请参阅我们的博文

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM