Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set file permissions on amazon s3 in iOS

I'm uploading a file to my S3 bucket, and I want it to have public read permission. It seems like it should be simple, but like many things in my life, it is not. Looking for answers, the closest hint I've found is:

Add a header to your PUT request: x-amz-acl: public-read

which seems simple enough, but I'm using AWSS3TransferUtitlity, not PUT, and trying to set the requestHeader on the AWSS3TransferUtilityUploadExpression fails - setValue: forRequestHeader: is not recognized so I'm using requestParamaters instead, but that doesn't work either. I've searched and read till my eyes are bleary. Can someone help, please?

My code so far:

    AWSS3TransferUtilityUploadExpression *expression = [AWSS3TransferUtilityUploadExpression new];
 //   expression.uploadProgress = self.uploadProgress;
    [expression.requestParameters setValue:@"public-read" forKey:@"x-amz-acl"];
    AWSTask *atask;
    AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];
    NSLog(@"started file trans: %@", self.fileToSend.fileURL);
    [atask = [transferUtility uploadFile:self.fileToSend.fileURL
                                  bucket:S3BucketName
                                    key:self.fileToSend.fileName
                             contentType:self.fileToSend.mimeType
                              expression:expression
                         completionHander:self.completionHandler]
                        continueWithBlock:^id(AWSTask *task) {

EDIT: Some progress. I found I was using an outdated version of the SDK and uptodate version of the documentation. Another dumb mistake. Anyway, after updating the SDK and recompiling, it runs, but creates and exception 'NSUnknownKeyException', reason: '[<__NSDictionary0 0x12ed0c280> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key x-amz-acl.'

EDIT: OK. Fixed. With combination of hints, a step back, I changed my request header line to :

[expression setValue:@"public-read" forRequestHeader:@"x-amz-acl"];

Thanks to the responders.

like image 651
Ben Barlow Avatar asked Oct 30 '22 02:10

Ben Barlow


1 Answers

In swift use like this,

let expression = AWSS3TransferUtilityUploadExpression()
expression.setValue("public-read", forRequestHeader:"x-amz-acl")
like image 160
Sureshkumar Linganathan Avatar answered Nov 15 '22 05:11

Sureshkumar Linganathan