拷贝Object
更新时间:2022-10-21
简单拷贝Object
基本流程
- 创建BOSClient类的实例。
- 执行BOSClient copyObject方法。
- 返回BOSCopyObjectResponse类实例,可通过eTag/lastModified等属性获取eTag和最后修改时间。
示例代码
BOSCopyObjectRequest* request = [[BOSCopyObjectRequest alloc] init];
request.bucket = @"<bucketname>";
request.key = @"<objectname>";
request.source = @"<sourceBucket>/<sourceObject";
__block BOSCopyObjectResponse* response = nil;
BCETask* task = [client copyObject:request];
task.then(^(BCEOutput* output) {
if (output.response) {
response = (BOSCopyObjectResponse*)output.response;
NSLog(@"copy obj success!");
}
if (output.error) {
NSLog(@"copy obj failure");
}
});
[task waitUtilFinished];
完整示例
#import <BaiduBCEBasic/BaiduBCEBasic.h>
#import <BaiduBCEBOS/BaiduBCEBOS.h>
void example(void) {
// 初始化
BCECredentials* credentials = [[BCECredentials alloc] init];
credentials.accessKey = @"<access key>";
credentials.secretKey = @"<secret key>";
BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
configuration.credentials = credentials;
BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
BOSCopyObjectRequest* request = [[BOSCopyObjectRequest alloc] init];
request.bucket = @"<bucketname>";
request.key = @"<objectname>";
request.source = @"<sourceBucket>/<sourceObject";
__block BOSCopyObjectResponse* response = nil;
BCETask* task = [client copyObject:request];
task.then(^(BCEOutput* output) {
if (output.response) {
response = (BOSCopyObjectResponse*)output.response;
NSLog(@"copy obj success!");
}
if (output.error) {
NSLog(@"copy obj failure");
}
});
[task waitUtilFinished];
}
说明:copyObject 方法返回一个
BOSCopyObjectResponse
对象,该对象中包含了新Object的ETag和修改时间。
指定条件拷贝Object
您也可以通过指定条件来实现Object的拷贝。该功能一般用于如下场景:
- Copy一个Object但重新设置meta。
- 重置某个现有Object的meta(把源和目标设置为同一个Object)。
- 当源Object的eTag与指定的eTag相同时复制;
- 当源Object的eTag与指定的eTag不相同时复制;
- 当源Object的在指定的时间后没有被修改时复制;
- 当源Object的在指定的时间后被修改过时复制;
具体内容如下:
基本流程
- 创建BOSCopyObjectRequest类的实例,传入
<source>
,<ifMatchEtag>
,<ifNotMatchEtag>
,<ifModifiedSince>
,<ifUnmodifiedSince>
,<metadataDirective>
参数。 - 返回BOSCopyObjectResponse类实例,可通过eTag/lastModified属性获取eTag和最后修改时间。
示例代码
BOSCopyObjectRequest* request = [[BOSCopyObjectRequest alloc] init];
request.bucket = @"<bucketname>";
request.key = @"<objectname>";
request.source = @"<sourceBucket>/<sourceObject";
request.metadataDirective = @"replace";
request.ifModifiedSince = @"Wed, 01 Mar 2006 12:00:00 GMT";
__block BOSCopyObjectResponse* response = nil;
BCETask* task = [client copyObject:request];
task.then(^(BCEOutput* output) {
if (output.response) {
response = (BOSCopyObjectResponse*)output.response;
NSLog(@"copy obj success!");
}
if (output.error) {
NSLog(@"copy obj failure");
}
});
[task waitUtilFinished];
说明:
BOSCopyObjectRequest
允许用户修改目的Object的ObjectMeta,同时也提供MatchingETagConstraints
参数的设定。