上传文件到AWS S3存储桶。

时间:2021-11-14 01:32:30

I have setup S3 bucket in US West i.e N. California

我在美国西部安装了S3桶。e n .加州

My Pool-ID start with "us-east-1:*****"

我的Pool-ID以“us-east-1:***** **”开头

I have following code to upload file on my bucket named AWS_BUCKET_NAME

我有下面的代码来上传我的bucket AWS_BUCKET_NAME文件

let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USWest1, identityPoolId: AWS_IDENTITY_POOL_ID)
    let configuration = AWSServiceConfiguration(region: .USWest1, credentialsProvider: credentialProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration
    let uploadingFileURL = videoURL
    let uploadRequest = AWSS3TransferManagerUploadRequest()
    uploadRequest?.bucket = AWS_BUCKET_NAME
    uploadRequest?.key = fileName
    uploadRequest?.body = uploadingFileURL
    uploadRequest?.contentType = "video/mp4"

    let transferManager = AWSS3TransferManager.default()
    transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread())
    { (task) -> Any? in
        if task.error == nil
        {
            return nil
        }
        else
        {
            print("Error: \(String(describing: task.error))")
        }
        return nil
    }

Mostly it successfully upload the video files. But sometimes its giving error like bellow

主要是成功上传视频文件。但有时它会产生错误,如咆哮

Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x608001a53c50 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=8, kCFStreamErrorDomainKey=12}}, NSErrorFailingURLStringKey=https://cognito-identity.us-west-1.amazonaws.com/, NSErrorFailingURLKey=https://cognito-identity.us-west-1.amazonaws.com/, kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, NSLocalizedDescription=A server with the specified hostname could not be found.}

错误域=NSURLErrorDomain Code=-1003“无法找到具有指定主机名的服务器”。UserInfo={NSUnderlyingError=0x608001a53c50 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)"UserInfo={_kCFStreamErrorCodeKey=8, kalistreamerrordomainkey =12}, NSErrorFailingURLStringKey=https://cognito-identity.us-west-1.amazonaws.com/,

Now if i change the regionType in AWSCognitoCredentialsProvider to USEast1 or some other and try again then its again give me same error but after that if i set "USWest1" region again then its working again. It will continue to work successful for 5-6 times or sometimes even more. But again after sometime it generating same error.

如果我将AWSCognitoCredentialsProvider中的regionType改为USEast1或其他类型然后再试一次,它又会给我同样的错误,但之后如果我再次设置USWest1区域,它就会再次工作。它将持续成功5-6次,有时甚至更多。但是过了一段时间,它又产生了同样的错误。

One more thing, with same pool id and region, Its working fine in android. only facing this issue in iOS.

还有一点,拥有相同的池id和区域,在android中运行良好。只有在iOS中才会遇到这个问题。

Thanks in advance.

提前谢谢。

1 个解决方案

#1


1  

Finally i found the solution for this. This is not proper answer but it may help someone else who are facing such issue.

最后我找到了解决办法。这不是一个恰当的答案,但它可能会帮助其他人面对这样的问题。

let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: AWS_IDENTITY_POOL_ID)
    let configuration = AWSServiceConfiguration(region: .USWest1, credentialsProvider: credentialProvider)

After Michael - sqlbot's comment, i have checked by changing Region one after another. Finally i am creating AWSCognitoCredentialsProvider with .USEast1 and configuring AWSServiceConfiguration with .USWest1 and it is working now.

在Michael - sqlbot的评论之后,我检查了一个又一个变化的区域。最后,我使用. useast1创建了AWSCognitoCredentialsProvider,并使用. uswest1配置了AWSServiceConfiguration,它现在正在工作。

I don't know the reason behind but probably we need to configure AWSServiceConfiguration with the region of bucket we are using.

我不知道背后的原因,但是我们可能需要使用bucket的区域来配置AWSServiceConfiguration。

#1


1  

Finally i found the solution for this. This is not proper answer but it may help someone else who are facing such issue.

最后我找到了解决办法。这不是一个恰当的答案,但它可能会帮助其他人面对这样的问题。

let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: AWS_IDENTITY_POOL_ID)
    let configuration = AWSServiceConfiguration(region: .USWest1, credentialsProvider: credentialProvider)

After Michael - sqlbot's comment, i have checked by changing Region one after another. Finally i am creating AWSCognitoCredentialsProvider with .USEast1 and configuring AWSServiceConfiguration with .USWest1 and it is working now.

在Michael - sqlbot的评论之后,我检查了一个又一个变化的区域。最后,我使用. useast1创建了AWSCognitoCredentialsProvider,并使用. uswest1配置了AWSServiceConfiguration,它现在正在工作。

I don't know the reason behind but probably we need to configure AWSServiceConfiguration with the region of bucket we are using.

我不知道背后的原因,但是我们可能需要使用bucket的区域来配置AWSServiceConfiguration。