9

バケットの数に制限があるように思われるので、次のことを実行する方法を見つけようとしています。

  • ユーザーがプロフィール画像をアップロードできるiOSアプリを持っています。
  • プロフィール画像は誰でも閲覧できます(公開したい)。
  • 理想的には、単一のバケットにアップロードできます(例:myprofilepics.s3.amazonaws.com)
  • 理想的には、各ユーザーは自分のサブフォルダーにアップロードできます(例:myprofilepics.s3.amazonaws.com/images/userXXX/
  • 理想的には、画像をアップロードし、アプリから直接パブリックアクセスに設定して、他のユーザーがすぐにプロフィール写真を表示できるようにします。

ドキュメントに何かが欠けていますか?この問題に関するフィードバックに感謝します。

4

3 に答える 3

21

この問題を解決するために、私は Amazon の iOS SDK のサンプル コード (ここにある) から始めました。SDK zip では、目的のサンプル プロジェクトが にありますsamples/S3_Uploader

そのサンプル プロジェクトから、アップロードされた画像が公開されているプロジェクトに移行するには、適切な場所に 1 行を追加するだけです。

por.cannedACL   = [S3CannedACL publicRead];

は画像のアップロードに使用されますporS3PutObjectRequest

アップロード用の私のプロジェクトのコードは次のようになります (Amazon のサンプル コードとほぼ同じに見えます)。

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、画像バケットです。

于 2012-08-13T19:23:30.080 に答える
1

自分のユーザーの ID を管理している場合 (つまり、AIM を使用していない場合)、アクセスできるリソースも管理する必要があります。S3 上のファイルへのアクセスを管理するシン Web サービスを挿入します。

または、一時的なセキュリティ認証情報とセキュリティ ポリシーを使用して、ユーザーに S3 へのアクセスを提供することもできます。ユーザーの一時トークンを取得するには、デバイスにコードを追加する必要があります。

Amazon S3 を方程式から除外し、後で別のバックエンドを自由に選択できるようにするため、最初のソリューションを選択します。

于 2012-06-14T21:32:26.813 に答える
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;
于 2012-12-28T21:08:19.413 に答える