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";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";
// 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";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";
// 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 is changed after the time specified by this parameter; otherwise, an exception is thrown. | cpSrcIfModifiedSince |
| Copy-Source-if-Unmodified-Since | Copies the source object if it is changed before the time specified by this parameter; 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";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";
// 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";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";
// 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);
}] ; Last Article: Deleting an Object
Next Article: Temporarily Authorized Access
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.