拷贝对象
对象是存储数据的基本单元。对象由元信息(Object Meta),用户数据(Data)和文件名(Key)组成。对象由桶内部唯一的Key来标识。本文介绍如何拷贝对象。
接口定义:
void S3_copy_object(const S3BucketContext *bucketContext,
const char *key, const char *destinationBucket,
const char *destinationKey,
const S3PutProperties *putProperties,
int64_t *lastModifiedReturn, int eTagReturnSize,
char *eTagReturn, S3RequestContext *requestContext,
int timeoutMs,
const S3ResponseHandler *handler, void *callbackData);
参数:
参数名 | 类型 | 说明 |
---|---|---|
bucketContext | const S3BucketContext * | 源bucket及请求参数 |
key | const char * | 源对象名 |
destinationBucket | const char * | 目的bucket |
destinationKey | const char * | 目的对象名 |
putProperties | const S3PutProperties * | 请求消息头 |
lastModifiedReturn | int64_t * | 对象上次修改时间 |
eTagReturnSize | int | eTag缓存区大小 |
eTagReturn | char * | eTag缓存区 |
requestContext | S3RequestContext * | 请求参数,如果为NULL,则立即同步执行请求 |
timeoutMs | int | 如果非0,则是以毫秒为单位的请求超时时间 |
handler | const S3ResponseHandler * | 回调函数 |
callbackData | void * | 回调数据 |
代码示例:
#include <time.h>
#include <unistd.h>
void copy_object(const char *sourceBucketName, const char *sourceKey,
const char *destinationBucketName, const char *destinationKey)
{
const char *cacheControl = 0, *contentType = 0;
const char *contentDispositionFilename = 0, *contentEncoding = 0;
int64_t expires = -1;
S3CannedAcl cannedAcl = S3CannedAclPrivate;
int metaPropertiesCount = 0;
S3NameValue metaProperties[S3_MAX_METADATA_COUNT];
int anyPropertiesSet = 0;
S3PutProperties putProperties =
{
contentType,
0,
cacheControl,
contentDispositionFilename,
contentEncoding,
expires,
cannedAcl,
metaPropertiesCount,
metaProperties};
S3ResponseHandler responseHandler =
{
&responsePropertiesCallback,
&responseCompleteCallback};
S3BucketContext bucketContext2 =
{
hostname,
sourceBucketName,
S3ProtocolHTTP,
S3UriStylePath,
accessKeyG,
secretKeyG,
NULL,
NULL};
int64_t lastModified;
char eTag[256];
do
{
S3_copy_object(&bucketContext2, sourceKey, destinationBucketName,
destinationKey, anyPropertiesSet ? &putProperties : 0,
&lastModified, sizeof(eTag), eTag, 0, 0,
&responseHandler, 0);
} while (S3_status_is_retryable(statusG) && should_retry());
if (statusG == S3StatusOK)
{
if (lastModified >= 0)
{
char timebuf[256];
time_t t = (time_t)lastModified;
strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ",
gmtime(&t));
printf("Last-Modified: %s\n", timebuf);
}
if (eTag[0])
{
printf("ETag: %s\n", eTag);
}
}
else
{
printError();
}
}