我使用最新的亞馬遜SDK庫將圖片上傳到存儲桶。但得到的錯誤,這裏是我的代碼如何在AWS s3上傳圖片,上傳時出錯
應用程序代理代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionAPSoutheast1
identityPoolId:AWS_IDENTITY_POOL_ID1];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionAPSoutheast1
credentialsProvider:credentialsProvider];
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
return YES;
}
我的視圖控制器代碼是
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
NSString *downloadingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"downloaded-myImage.jpg"];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];
imageView.image = [UIImage imageWithContentsOfFile:downloadingFilePath];
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = @"myBucketName";
uploadRequest.key = @"downloaded-myImage.jpg";
uploadRequest.body = downloadingFileURL;
[[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor]
withBlock:^id(AWSTask *task) {
if (task.error) {
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
switch (task.error.code) {
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:
break;
default:
NSLog(@"Error: %@", task.error);
break;
}
} else {
// Unknown error.
NSLog(@"Error: %@", task.error);
}
}
if (task.result) {
AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
// The file uploaded successfully.
NSLog(@"LOG %@", task.result);
}
return nil;
}];
[transferManager upload:uploadRequest];
但是這樣
AWSiOSSDKv2 [Error] AWSCredentialsProvider.m line:528 | __40-[AWSCognitoCredentialsProvider refresh]_block_invoke352 | Unable to refresh.
Error is [Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found."
UserInfo={NSUnderlyingError=0x7ffd7300a070 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)"
UserInfo={_kCFStreamErrorCodeKey=8, _kCFStreamErrorDomainKey=12}}, NSErrorFailingURLStringKey=https://cognito-identity.ap-southeast-1.amazonaws.com/,
NSErrorFailingURLKey=https://cognito-identity.ap-southeast-1.amazonaws.com/, _kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8,
NSLocalizedDescription=A server with the specified hostname could not be found.}]
您是如何驗證身份的?你通過Cognito提供臨時訪問? – Tom
@Tom我只是編輯我的代碼 –