如何上传Image AWSS3 TransferManager上传请求目标c?

时间:2017-05-05 12:17:39

标签: ios iphone amazon-web-services

我正在开发一个应用程序,我需要使用AWSS3上传图像。以下是我的代码。但是我收到有关未找到池ID的错误。我不知道该怎么回事,我还需要添加更多内容。

这是错误获取

错误Domain = com.amazonaws.AWSCognitoIdentityErrorDomain Code = 10“(null)”UserInfo = {__ type = ResourceNotFoundException,message = IdentityPool'ap-northeast-1:xxxxxxx'not found

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@""];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;

// get the image from a UIImageView that is displaying the selected Image

// create a local image that we can use to upload to s3
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"image.png"];
NSData *imageData = UIImagePNGRepresentation(selectedImage);
[imageData writeToFile:path atomically:YES];

// once the image is saved we can use the path to create a local fileurl
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];

// next we set up the S3 upload request manager
AWSS3TransferManagerUploadRequest *_uploadRequest = [AWSS3TransferManagerUploadRequest new];
// set the bucket
_uploadRequest.bucket = @"chatify";
// I want this image to be public to anyone to view it so I'm setting it to Public Read
_uploadRequest.ACL = AWSS3ObjectCannedACLPublicRead;
// set the image's name that will be used on the s3 server. I am also creating a folder to place the image in
_uploadRequest.key = @"ios/image.png";
// set the content type
_uploadRequest.contentType = @"image/png";
// we will track progress through an AWSNetworkingUploadProgressBlock
_uploadRequest.body = url;

__weak ClaimViewController *weakSelf = self;

_uploadRequest.uploadProgress =^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend){
    dispatch_sync(dispatch_get_main_queue(), ^{

        weakSelf.amountUploaded = totalBytesSent;
        weakSelf.filesize = totalBytesExpectedToSend;
        [weakSelf update];

    });
};

// now the upload request is set up we can creat the transfermanger, the credentials are already set up in the app delegate
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
// start the upload
[[transferManager upload:_uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) {

    // once the uploadmanager finishes check if there were any errors
    if (task.error) {
        NSLog(@"%@", task.error);
    }else{// if there aren't any then the image is uploaded!
        // this is the url of the image we just uploaded
        NSLog(@"https://s3.amazonaws.com/s3-demo-objectivec/foldername/image.png");
    }
    return nil;
}];

2 个答案:

答案 0 :(得分:0)

hi @kashif,错误清除表示您需要添加池ID

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@""];

但在上面的代码中,您只需传递identityPoolId中的Black

这里还提供了Credential

的另一种方法
AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:@"AccessKey"  secretKey :@"secretKey"];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2
                                                                         credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

答案 1 :(得分:0)

此错误是因为您的帐户中删除了您的Cognito Identity Pool,或者您错误地设置了该区域。您的地区应设置为 AWSRegionAPNorthEast1基于我在发布的代码中看到的内容。

谢谢, 罗汉

相关问题