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

时间:2012-06-14 19:11:10

标签: ios image file-upload permissions amazon-s3

由于我们似乎只限于桶的数量,我正在试图弄清楚如何完成以下任务:

  • 我有一个iOS应用,用户可以上传个人资料图片。
  • 任何人都可以查看个人资料图片(我希望公开)。
  • 理想情况下,我可以上传到一个存储桶(例如:myprofilepics.s3.amazonaws.com)
  • 理想情况下,每个用户都可以上传到自己的子文件夹(例如:myprofilepics.s3.amazonaws.com/images/userXXX /
  • 理想情况下,我上传图片,并将其设置为直接从应用程序进行公开访问,以便其他用户可以立即查看个人资料照片。

我遗漏了文档中的内容吗?我感谢您对此问题的任何反馈。

3 个答案:

答案 0 :(得分:21)

为了解决这个问题,我在他们的iOS SDK中找到了亚马逊的示例代码,找到了here。在SDK zip中,可以在samples/S3_Uploader找到感兴趣的示例项目。

要从该示例项目获取上传图像公开的项目,您只需在正确的位置添加一行:

por.cannedACL   = [S3CannedACL publicRead];

其中por是用于上传图片的S3PutObjectRequest

我的项目上传代码如下所示(看起来几乎与亚马逊的示例代码相同):

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_IDAWS_SECRET_KEY当然是您的AWS凭据,而AWS_PICTURE_BUCKET是您的图片包。

答案 1 :(得分:1)

如果您正在管理自己用户的身份(即不是通过AIM),您还必须管理可以访问的资源。我会使用一个瘦Web服务来管理对S3上文件的访问。

或者,您可以使用Temporary Security Credentials和安全策略为您的用户提供S3访问权限。您必须在设备上添加代码才能为您的用户获取临时令牌。

我选择第一个解决方案,因为它让亚马逊S3不受影响,你的手可以在后期自由选择另一个后端。

答案 2 :(得分:0)

这就是我最终做的事情:

    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;