AWS S3存储桶上传映像问题

时间:2018-09-24 10:02:49

标签: ios swift amazon-web-services amazon-s3

当我在s3存储桶中上传图片时。我遇到了错误。我已经在堆栈溢出中看到此错误,但是没有得到。你能指导我吗?在哪个阶段我犯了错误。

  

错误:错误Domain = com.amazonaws.AWSS3ErrorDomain代码= 0“(null)” UserInfo = {RequestId = 9524E756031421C5,Bucket = xxxxxx-xxxxxx-xxxxx,HostId = uxxxxxxxxxx,Message =您要访问的存储桶必须使用指定的端点进行寻址。请将所有以后的请求发送到此终结点。,代码= PermanentRedirect,终结点= xxxxxx-xxxxxx-xxxxx.s3.amazonaws.com}

我在下面的plist中设置了poolid其他功能。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>amazonaws.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>amazonaws.com.cn</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>
<key>AWS</key>
<dict>
    <key>CredentialsProvider</key>
    <dict>
        <key>CognitoIdentity</key>
        <dict>
            <key>Default</key>
            <dict>
                <key>PoolId</key>
                <string>us-east-1:xxxxxxxxxxxxx</string>
                <key>Region</key>
                <string>us-east-1</string>
            </dict>
        </dict>
    </dict>
    <key>S3TransferManager</key>
    <dict>
        <key>Default</key>
        <dict>
            <key>Region</key>
            <string>us-east-1</string>
        </dict>
    </dict>
    <key>S3</key>
    <dict>
        <key>Default</key>
        <dict>
            <key>Region</key>
            <string>us-east-1</string>
        </dict>
    </dict>
</dict>

我在下面的代码中设置了上传图片的功能。

 let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1,
                                                            identityPoolId:"us-east-1:xxxxxxxxxxxx")
    let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration
    AWSS3TransferManager.register(with: configuration!, forKey: "USEast1S3TransferManager")

    self.transferManager = AWSS3TransferManager.default()


func uploadImage(){

    if let filepath = Bundle.main.path(forResource: "temp", ofType: "jpg") {

        let imageData = Data()

        let fileUrl = NSURL(fileURLWithPath: filepath)


        do {
            try imageData.write(to: fileUrl as URL, options: Data.WritingOptions.atomic)
        } catch let error as NSError {
            print("Could not save \(error), \(error.userInfo)")
        }


        let uploadRequest = AWSS3TransferManagerUploadRequest()
        uploadRequest?.bucket = bucketName
        uploadRequest?.key = "key.jpg"
        uploadRequest?.contentType = "image/jpg"
        uploadRequest?.body = fileUrl as URL


        transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in

            if let error = task.error as NSError? {
                if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
                    switch code {
                    case .cancelled, .paused:
                        break
                    default:
                        print("Error uploading: \(String(describing: uploadRequest?.key!) ) Error: \(error)")
                    }
                } else {
                    print("Error uploading: \(String(describing: uploadRequest?.key!)) Error: \(error)")
                }
                return nil
            }

            let uploadOutput = task.result
            print("Upload complete for: \(uploadRequest?.key!)")
            return nil
        })

    }

}

我想向您说明的另一件事是,是否需要在上面的代码或PoolId中为此设置“访问密钥”和“秘密密钥”。

感谢

0 个答案:

没有答案