iOS 接入AWS S3存储桶 Amazon

查看了官网的教程 始终无法成功接入AWS S3的存储桶
后台把相关的js代码发过来,我改成对应的iOS代码之后,成功接入了
记得要访问AWS的设备需要打开科学上网
上传文件代码

        let providerToken = AWSBasicSessionCredentialsProvider(accessKey: "你的accessKey", secretKey: "你的secretKey", sessionToken: "你的sessionToken")
        let configuration = AWSServiceConfiguration(region: .APSoutheast2, credentialsProvider: providerToken)
        AWSServiceManager.default()?.defaultServiceConfiguration = configuration

        let key = "test.pdf"
        let request = AWSS3PutObjectRequest()
        request?.bucket = "你的bucket"
        request?.key = key
        request?.contentType = "application/pdf"

        let data2 = NSData(contentsOfFile: path)
        let data = data2 as! Data
        let size = getFileSize(path: path)
        request?.contentLength = NSNumber(value: size)
        request?.body = data
        AWSS3.default().putObject(request!) { (output, error) in
            if(error == nil){
                print("fancy_log:upload success:\(key)")
            }else{
                print("fancy_log:upload fail!")
            }
        }

下载文件

        let request = AWSS3GetObjectRequest()
        request?.bucket = "ctrldoc-s3-1st-test"
        request?.key = "test.pdf"
        AWSS3.default().getObject(request!) { (output, error) in
            
        }

Tips:还有其它配置方式,注意accessKey和secretKey要使用管理员的

        let provider = AWSStaticCredentialsProvider(accessKey: "你的accessKey", secretKey: "你的secretKey")
        let configuration = AWSServiceConfiguration(region: .APSoutheast2, credentialsProvider: provider)
        AWSServiceManager.default()?.defaultServiceConfiguration = configuration

你可能感兴趣的:(iOS 接入AWS S3存储桶 Amazon)