在iOS应用程序中删除S3存储桶中的文件(Swift)

时间:2016-07-04 17:58:12

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

我正在Swift中编写iOS应用程序,并使用Amazon S3存储文件。我想知道是否有人知道如何以编程方式删除我的S3存储桶中的文件命令(而不是在存储桶生命周期中设置删除策略)。

提前致谢

2 个答案:

答案 0 :(得分:13)

从S3存储桶中删除文件的代码应如下所示:

    let s3 = AWSS3.defaultS3()
    let deleteObjectRequest = AWSS3DeleteObjectRequest()
    deleteObjectRequest.bucket = "yourBucketName"
    deleteObjectRequest.key = "yourFileName"
    s3.deleteObject(deleteObjectRequest).continueWithBlock { (task:AWSTask) -> AnyObject? in
        if let error = task.error {
            print("Error occurred: \(error)")
            return nil
        }
        print("Deleted successfully.")
        return nil
    }

谢谢, 罗汉

答案 1 :(得分:0)

快速4 +

如果您还不是最初的人

let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.APSoutheast1, identityPoolId: "yourPoolID")

let configuration = AWSServiceConfiguration(region:.APSoutheast1, credentialsProvider:credentialsProvider)

AWSServiceManager.default().defaultServiceConfiguration = configuration

let s3 = AWSS3.default()
guard let deleteObjectRequest = AWSS3DeleteObjectRequest() else {
    return
}
deleteObjectRequest.bucket = "yourBucketName"
deleteObjectRequest.key = "yourFileName"
s3.deleteObject(deleteObjectRequest).continueWith { (task:AWSTask) -> AnyObject? in
    
    if let error = task.error {
        print("Error occurred: \(error)")
        return nil
    }
    print("Deleted successfully.")
    return nil
    
}