OGeek|极客世界-中国程序员成长平台

标题: ios - Amazon S3 使用 Objective C 重命名对象 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:01
标题: ios - Amazon S3 使用 Objective C 重命名对象

如果有一种方法可以使用 Objective C 从 Amazon S3 的存储桶中重命名文件夹(对象),我很感兴趣。

根据我的阅读,这是不可能的,相反,我发现我应该创建一个新目录并将每个项目从原始目录复制到新目录,最后我应该删除原始目录(对象)。

任何人都可以提供任何案例的例子吗?

更新 1:

想过做这样的事情,在同一个桶中复制同一个对象,但我得到一个错误:

请求失败。错误:操作无法完成。 (com.amazonaws.AWSServiceErrorDomain 错误 11。)

- (void)test{

    AWSS3 *s3 = [AWSS3 defaultS3];
    AWSS3ReplicateObjectRequest* replicateRequest = [AWSS3ReplicateObjectRequest new];
    replicateRequest.bucket = kAmazonS3Bucket;
    //folder to copy the .txt file from
    replicateRequest.key = @"backup/folder2/file.txt";
    //original .txt file location
    replicateRequest.replicateSource = @"backup/folder1/file.txt";
    replicateRequest.ACL = AWSS3ObjectCannedACLPublicReadWrite;

    [s3 replicateObject:replicateRequest
      completionHandler:^(AWSS3ReplicateObjectOutput * _Nullable response, NSError * _Nullable error) {
          if(error){
              NSLog(@"The request failed. error: %@", error.localizedDescription);
          }
      }];

}

更新 2:

这是我用来从亚马逊删除文件的实现,请注意 dele 文件夹不起作用。

/**

 Use this function to delete item at path inside a bucket
 @autor Daisoreanu Laurentiu
 @param filePath The path for the file that should be removed. Example of path: `backup/test.txt`

 */
-(void)deleteObjectAtPathNSString *)filePath
               completionvoid (^)(BOOL success))completionBlock{

    [self setupAmazon];

    AWSS3 *s3 = [AWSS3 defaultS3];
    AWSS3DeleteObjectRequest *deleteRequest = [AWSS3DeleteObjectRequest new];
    deleteRequest.bucket = kAmazonS3Bucket;
    deleteRequest.key = filePath;

    [s3 deleteObject:deleteRequest
   completionHandler:^(AWSS3DeleteObjectOutput * _Nullable response, NSError * _Nullable error) {
       if(error){
           NSLog(@"The request failed. error: %@", error.localizedDescription);
           completionBlock(NO);
       }
       else{
           completionBlock(YES);
       }
   }];


}



Best Answer-推荐答案


查看这些 REST API 示例:

复制对象:

PUT /destinationObject HTTP/1.1
Host: destinationBucket.s3.amazonaws.com
x-amz-copy-source: /source_bucket/sourceObject
x-amz-metadata-directive: metadata_directive
x-amz-copy-source-if-match: etag
x-amz-copy-source-if-none-match: etag
x-amz-copy-source-if-unmodified-since: time_stamp
x-amz-copy-source-if-modified-since: time_stamp
<request metadata>
Authorization: authorization string (see Authenticating Requests (AWS Signature Version
    4))
Date: date

删除对象:

DELETE /ObjectName HTTP/1.1
Host: BucketName.s3.amazonaws.com
Date: date
Content-Length: length
Authorization: authorization string (see Authenticating Requests (AWS Signature Version
    4))

更多信息在这里:

http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectDELETE.html

关于ios - Amazon S3 使用 Objective C 重命名对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38477494/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4