拷贝Object
更新时间:2022-10-21
简单拷贝Object
基本流程
- 创建BOSClient 类的实例。
- 执行BOSClient.copyObject( )方法。
- 返回CopyObjectResponse类实例,可通过getETag()/getLastModified()获取eTag和最后修改时间。
示例代码
// 拷贝Object
CopyObjectResponse copyObjectResponse = client.copyObject(<SrcBucketName>, <SrcKey>, <DestBucketName>, <DestKey>); //SrcBucketName, SrcKey为原地址,DestBucketName, DestKey为拷贝到的目的地址
// 打印结果
System.out.println("ETag: " + copyObjectResponse.getETag() + " LastModified: " + copyObjectResponse.getLastModified());
完整示例
import android.app.Activity;
import android.os.Bundle;
import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.demo.R;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.CopyObjectResponse;
public class ExampleActivity extends Activity {
private String srcBucketName = <SrcBucketName>;
private String srcKey = <SrcKey>;
private String destBucketName = <DestBucketName>;
private String destKey = <DestKey>;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
try {
BosClientConfiguration config = new BosClientConfiguration();
config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
config.setEndpoint(<EndPoint>);
BosClient client = new BosClient(config);
// 拷贝Object
CopyObjectResponse copyObjectResponse = client.copyObject(<SrcBucketName>, <SrcKey>, <DestBucketName>, <DestKey>);
// 打印结果
System.out.println("ETag: " + copyObjectResponse.getETag() + " LastModified: " + copyObjectResponse.getLastModified());
} catch (BceServiceException e) {
System.out.println("Error ErrorCode: " + e.getErrorCode());
System.out.println("Error RequestId: " + e.getRequestId());
System.out.println("Error StatusCode: " + e.getStatusCode());
System.out.println("Error Message: " + e.getMessage());
System.out.println("Error ErrorType: " + e.getErrorType());
} catch (BceClientException e) {
System.out.println("Error Message: " + e.getMessage());
}
}
}).start();
}}
说明:copyObject 方法返回一个
CopyObjectResponse
对象,该对象中包含了新Object的ETag和修改时间。
通过CopyObjectRequest拷贝Object
您也可以通过 CopyObjectRequest
实现Object的拷贝。该功能一般用于如下场景:
- Copy一个Object但重新设置meta。
- 重置某个现有Object的meta(把Src和Des设置为同一个Object)。
基本流程
- 创建CopyObjectRequest类的实例,传入
<SrcBucketName>
,<SrcKey>
,<DestBucketName>
,<DestKey>
参数。 - 创建ObjectMetadata类的实例。
- 返回CopyObjectResponse类实例,可通过getETag()/getLastModified()获取eTag和最后修改时间。
示例代码
// 创建CopyObjectRequest对象
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(<SrcBucketName>, <SrcKey>, <DestBucketName>, <DestKey>);
// 设置新的Metadata
Map<String, String> userMetadata = new HashMap<String, String>();
userMetadata.put(<UserMetaKey>,<UserMetaValue>);
meta.setUserMetadata(userMetadata);
copyObjectRequest.setNewObjectMetadata(meta);
// 复制Object
CopyObjectResponse copyObjectResponse = client.copyObject(copyObjectRequest);
System.out.println("ETag: " + copyObjectResponse.getETag() + " LastModified: " + copyObjectResponse.getLastModified());
说明:
CopyObjectRequest
允许用户修改目的Object的ObjectMeta,同时也提供MatchingETagConstraints
参数的设定。