0

我已使用答案从 s3 存储桶中删除文件

我需要执行的任务是当我上传一张新图片时,我需要删除前一张。所以上传后,我正在调用删除前一张图片。删除方法使我成功,但文件仍在存储桶中。所以现在,我可以看到这两个文件;前一个和新的也是。我错过了什么吗?

这是我的上传和删除代码

func uploadfile(image: UIImage, s3Key: String, bucketName: String, completionHandlerWithImageURL: @escaping(_ imageurl: String) -> ())
{
    let transfer_utility = S3TransferUtilityConfig().config()
    let expression = S3Expression().config()
    
    var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
    completionHandler = { (task, error) -> Void in
        debugPrint("Upload Completed")
    }
    
    let bucketName = bucketName
    guard let data = image.pngData() else{
        return
    }

    transfer_utility?.uploadData(data as Data, bucket: bucketName, key: s3Key, contentType: "image/png", expression: expression,
    completionHandler: completionHandler).continueWith
    {
        (task) -> AnyObject? in
        if let error = task.error {
            print(error.localizedDescription)
            completionHandlerWithImageURL("")
        }
        
        if let _ = task.result {
            debugPrint("Uploaded image successfully")
            
            if task.result != nil {
                let url = AWSS3.default().configuration.endpoint.url
                let publicURL = url?.appendingPathComponent(bucketName).appendingPathComponent(s3Key)
                if let absoluteString = publicURL?.absoluteString {
                    debugPrint("Image Url :\(absoluteString)")
                    completionHandlerWithImageURL(absoluteString)
                }
            } else {
                debugPrint("Image URL not generated")
                completionHandlerWithImageURL("")
            }
        }
        return nil
    }
}

func deleteExistingFile(s3Key: String, bucketName: String) {
    let s3 = AWSS3.default()
    guard let deleteObjectRequest = AWSS3DeleteObjectRequest() else { return }
    deleteObjectRequest.bucket = bucketName
    deleteObjectRequest.key = "/images/\(s3Key.components(separatedBy: "/").last ?? "")"
    
    s3.deleteObject(deleteObjectRequest).continueWith { (task:AWSTask) -> AnyObject? in
        if let error = task.error {
            print("Error deleting file on s3 bucket: \(error)")
            return nil
        } else {
            print("s3 file Deleted successfully. \(bucketName) \(s3Key)")
            return nil
        }
    }
}

这就是我调用这两种方法的方式。

S3Manager.shared.uploadfile(image: image, s3Key: self.img_key, bucketName: (company_profile["dot"] as? String ?? ""))
    { (imgUrl) in
        if imgUrl != ""
        {
            S3Manager.shared.deleteExistingFile(s3Key: self.profileUrl, bucketName: (company_profile["dot"] as? String ?? ""))
        ...
        ...
    }
}

上传图片路径: https ://s3.us-east-1.amazonaws.com/bucketNumber/images/img.png

我已经尝试使用完整路径和只有文件夹/图像路径的 deleteObjectRequest.key ,在这两种情况下,方法都会成功,但文件仍然存在于存储桶中。

deleteObjectRequest.key: https ://bucketName.s3.amazonaws.com/images/old_img.png

deleteObjectRequest.key: /images/old_img.png

4

0 回答 0