简体   繁体   English

如何使用AWS iOS SDK从设备上传图像并设置为public

[英]How to use AWS iOS SDK to upload image from device and set to public

Since it seems we are limited to the number of buckets, I'm trying to figure out how to accomplish the following: 由于我们似乎只限于桶的数量,我正在试图弄清楚如何完成以下操作:

  • I have an iOS app, where users can upload a profile image. 我有一个iOS应用程序,用户可以上传个人资料图片。
  • Profile image can be viewed by anyone (I want it public). 任何人都可以查看个人资料图片(我希望公开)。
  • Ideally, I can upload to a single bucket (for example: myprofilepics.s3.amazonaws.com) 理想情况下,我可以上传到一个桶(例如:myprofilepics.s3.amazonaws.com)
  • Ideally, Each user can upload to their own sub folder (for example: myprofilepics.s3.amazonaws.com/images/userXXX/ 理想情况下,每个用户都可以上传到自己的子文件夹(例如:myprofilepics.s3.amazonaws.com/images/userXXX/
  • Ideally, I upload the image, and set it to public access direct from the app so that other users can immediately view profile pics. 理想情况下,我上传图像,并将其直接从应用程序设置为公共访问,以便其他用户可以立即查看个人资料图片。

Am I missing something in the documentation? 我在文档中遗漏了什么吗? I appreciate any feedback on this issue. 我感谢您对此问题的任何反馈。

To solve this problem, I started with Amazon's sample code in their iOS SDK, found here . 为了解决这个问题,我开始在自己的iOS SDK亚马逊的示例代码,找到这里 In the SDK zip, the sample project of interest can be found at samples/S3_Uploader . 在SDK zip中,可以在samples/S3_Uploader找到感兴趣的示例项目。

To get from that sample project to one in which the uploaded image is public, you simply need to add one line in the right place: 要从该示例项目获取上传图像公开的项目,您只需在正确的位置添加一行:

por.cannedACL   = [S3CannedACL publicRead];

where por is the S3PutObjectRequest used to upload the image. 其中por是用于上传图像的S3PutObjectRequest

My project's code for uploading looks like this (looks almost identical to Amazon's sample code): 我的项目上传代码如下所示(看起来几乎与亚马逊的示例代码相同):

NSString *uuid = @""; // Generate a UUID however you like, or use something else to name your image.
UIImage *image; // This is the UIImage you'd like to upload.

// This URL is not used in the example, but it points to the file
// to be uploaded.
NSString *url = [NSString pathWithComponents:@[ @"https://s3.amazonaws.com/", AWS_PICTURE_BUCKET, uuid ]];

// Convert the image to JPEG data. Use UIImagePNGRepresentation for pngs
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

// Create the S3 Client.
AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:AWS_ACCESS_KEY_ID withSecretKey:AWS_SECRET_KEY];

@try {
    // Create the picture bucket.
    [s3 createBucket:[[S3CreateBucketRequest alloc] initWithName:AWS_PICTURE_BUCKET]];

    // Upload image data.  Remember to set the content type.
    S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:uuid inBucket:AWS_PICTURE_BUCKET];
    por.contentType = @"image/jpeg"; // use "image/png" here if you are uploading a png
    por.cannedACL   = [S3CannedACL publicRead];
    por.data        = imageData;
    por.delegate    = self; // Don't need this line if you don't care about hearing a response.

    // Put the image data into the specified s3 bucket and object.
    [s3 putObject:por];
}
@catch (AmazonClientException *exception) {
    NSLog(@"exception");
}

AWS_ACCESS_KEY_ID and AWS_SECRET_KEY are, of course, your AWS credentials, and AWS_PICTURE_BUCKET is your picture bucket. AWS_ACCESS_KEY_IDAWS_SECRET_KEY当然是您的AWS凭证, AWS_PICTURE_BUCKET是您的图片桶。

If you are managing the identity of your own users - that is, not through AIM - you also have to manage what resources that have access to. 如果您正在管理自己用户的身份(即不通过AIM),则还必须管理可以访问的资源。 I would slip in a thin web service that governs access to the files on S3. 我会介入一个瘦Web服务来管理对S3上文件的访问。

Alternatively you can provide access to S3 for your users by using Temporary Security Credentials and a security policy. 或者,您可以使用临时安全证书和安全策略为您的用户提供对S3的访问。 You would have to add code on the device to get the temporary tokens for your users. 您必须在设备上添加代码才能获得用户的临时令牌。

I'd opt for the first solution as it keeps Amazon S3 out of the equation and your hands free to choose another backend at a later stage. 我选择了第一个解决方案,因为它让亚马逊S3不受影响,你可以在后期自由选择另一个后端。

This is what I ended up doing: 这就是我最终做的事情:

    BOOL success = YES;

// Initialize the S3 Client.
AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];

@try {
    // Create the picture bucket.
    [s3 createBucket:[[S3CreateBucketRequest alloc] initWithName:HOSTED_BUCKET]];

    NSString *remoteImagePath = [self pathWithImageType:SFHostedImageTypeProfile identiefier:@""];


    // Upload image data.  Remember to set the content type.
    S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:remoteImagePath inBucket:HOSTED_BUCKET];
    por.contentType = @"image/png";

    por.data        = UIImagePNGRepresentation(image);

    // Put the image data into the specified s3 bucket and object.
    [s3 putObject:por];
}
@catch (AmazonClientException *exception) {
    //        [Constants showAlertMessage:exception.message withTitle:@"Upload Error"];
    NSLog(@"Save Hosted Image Exception: %@", exception.message);

    success = NO;
}

return success;

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

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