Updated on 2024-05-08 GMT+08:00

Copying an Object

The object copy operation can create a copy for an existing object in OBS.

You can call copyObject to copy an object. When copying an object, you can specify properties and ACL for it.

Copying an Object in Simple Mode

Sample code:

static OBSClient *client;
NSString *endPoint = @"your-endpoint";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
char* ak_env = getenv("AccessKeyID");
char* sk_env = getenv("SecretAccessKey");
NSString *AK = [NSString stringWithUTF8String:ak_env];
NSString *SK = [NSString stringWithUTF8String:sk_env];
    
// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
    
//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
    
// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];
    
OBSCopyObjectRequest *request = [[OBSCopyObjectRequest alloc]initWithSrcBucketName:@"source-bucketname" srcObjectKey:@"objectname1" dstBucketName:@"destination-bucketname" dstObjectKey:@"objectname2"];
    
[client copyObject:request completionHandler:^(OBSCopyObjectResponse *response, NSError *error){
    NSLog(@"%@",response);
}] ;

Rewriting Object Properties

The following sample code shows how to rewrite object properties.

static OBSClient *client;
NSString *endPoint = @"your-endpoint";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
char* ak_env = getenv("AccessKeyID");
char* sk_env = getenv("SecretAccessKey");
NSString *AK = [NSString stringWithUTF8String:ak_env];
NSString *SK = [NSString stringWithUTF8String:sk_env];
    
// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
    
//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
    
// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];
    
OBSCopyObjectRequest *request = [[OBSCopyObjectRequest alloc]initWithSrcBucketName:@"source-bucketname" srcObjectKey:@"objectname1" dstBucketName:@"destination-bucketname" dstObjectKey:@"objectname2"];
    
// Rewrite properties.
request.dstObjectMetaDirective = OBSMetaDirectiveCopy;
    
request.dstObjectStorageClass = OBSStorageClassStandard;
    
request.dstObjectWebsiteRedirectLocation = @"URL";

request.customContentType = @"video/mp4";
    
    
[client copyObject:request completionHandler:^(OBSCopyObjectResponse *response, NSError *error){
    NSLog(@"%@",response);
}] ; 

Copying an Object by Specifying Conditions

When copying an object, you can specify one or more restriction conditions. If the conditions are met, the object will be copied. Otherwise, an exception will be thrown and the copy will fail.

You can set the following conditions:

Parameter

Description

Enumeration Value in OBS iOS SDK

Copy-Source-if-Match

Copies the source object if its ETag is the same as the one specified by this parameter; otherwise, an exception is thrown.

cpSrcIfETagMatch

Copy-Source-if-None-Match

Copies the source object if its ETag is different from the one specified by this parameter; otherwise, an exception is thrown.

cpSrcIfETagNoneMatch

Copy-Source-if-Modified-Since

Copies the source object if it has been modified since the specified time; otherwise, an exception is thrown.

cpSrcIfModifiedSince

Copy-Source-if-Unmodified-Since

Copies the source object if it has not been modified since the specified time; otherwise, an exception is thrown.

cpSrcIfUnmodifiedSince

  • The ETag of the source object is the MD5 check value of the source object.
  • If Copy-Source-if-Unmodified-Since, Copy-Source-if-Match, Copy-Source-if-Modified-Since, or Copy-Source-if-None-Match is included and its specified condition is not met, an exception will be thrown.
  • Copy-Source-if-Modified-Since and Copy-Source-if-None-Match can be used together, and so do Copy-Source-if-Unmodified-Since and Copy-Source-if-Match.

Sample code:

static OBSClient *client;
NSString *endPoint = @"your-endpoint";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
char* ak_env = getenv("AccessKeyID");
char* sk_env = getenv("SecretAccessKey");
NSString *AK = [NSString stringWithUTF8String:ak_env];
NSString *SK = [NSString stringWithUTF8String:sk_env];
    
// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
    
//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
    
// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];
    
OBSCopyObjectRequest *request = [[OBSCopyObjectRequest alloc]initWithSrcBucketName:@"source-bucketname" srcObjectKey:@"objectname1" dstBucketName:@"destination-bucketname" dstObjectKey:@"objectname2"];
    
    
request.cpSrcIfETagNoneMatch = @"\"f807071206c05630b4d3c92aae4f4448\"";
    
request.cpSrcIfModifiedSince = @"Sunday, 06-Nov-94 08:49:37 GMT";
    
//request.cpSrcIfModifiedSince = [[OBSUtils      getDateFormatterRFC1123]dateFromString:@"Mon, 18 Dec 2017 03:50:49 GMT"];
    
[client copyObject:request completionHandler:^(OBSCopyObjectResponse *response, NSError *error){
    NSLog(@"%@",response);
}] ;

Modifying an Object ACL

Sample code:

static OBSClient *client;
NSString *endPoint = @"your-endpoint";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
char* ak_env = getenv("AccessKeyID");
char* sk_env = getenv("SecretAccessKey");
NSString *AK = [NSString stringWithUTF8String:ak_env];
NSString *SK = [NSString stringWithUTF8String:sk_env];
    
// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
    
//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
    
// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];
    
OBSCopyObjectRequest *request = [[OBSCopyObjectRequest alloc]initWithSrcBucketName:@"source-bucketname" srcObjectKey:@"objectname1" dstBucketName:@"destination-bucketname" dstObjectKey:@"objectname2"];
    
// Grant the FULL_CONTROL permission.
request.dstObjectACLPolicy = OBSACLFull_Control;
[client copyObject:request completionHandler:^(OBSCopyObjectResponse *response, NSError *error){
    NSLog(@"%@",response);
}] ;