上传Object
更新时间:2022-10-21
最简单的上传
基本流程
- 创建BOSClient类的实例。
- 调用BOSClient putObject方法,可以通过如下二种方式上传Object:文件、二进制数据的形式。
- 对返回的BOSPutObjectResponse类型实例,可以执行获取eTag操作。
示例代码
BOSObjectContent* content = [[BOSObjectContent alloc] init];
// 以文件方式
content.objectData.file = @"<file path>";
// 或者以二进制数据方式
NSData* data = [[NSData alloc] init];
content.objectData.data = data;
BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
request.bucket = @"<bucketname>";
request.key = @"<objectname>";
request.objectContent = content;
__block BOSPutObjectResponse* response = nil;
BCETask* task = [client putObject:request];
task.then(^(BCEOutput* output) {
if (output.progress) {
NSLog(@"put object progress is %@", output.progress);
}
if (output.response) {
response = (BOSPutObjectResponse*)output.response;
NSLog(@"put object success!");
}
if (output.error) {
NSLog(@"put object failure");
}
});
[task waitUtilFinished];
说明:Object以文件的形式上传到BOS中,putObject函数支持不超过5GB的Object上传。在putObject请求处理成功后,BOS会在Header中返回Object的ETag作为文件标识。
完整示例
#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];
BOSObjectContent* content = [[BOSObjectContent alloc] init];
// 以文件方式
content.objectData.file = @"<file path>";
// 或者以二进制数据方式
NSData* data = [[NSData alloc] init];
content.objectData.data = data;
BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
request.bucket = @"<bucketname>";
request.key = @"<objectname>";
request.objectContent = content;
__block BOSPutObjectResponse* response = nil;
BCETask* task = [client putObject:request];
task.then(^(BCEOutput* output) {
if (output.progress) {
NSLog(@"put object progress is %@", output.progress);
}
if (output.response) {
response = (BOSPutObjectResponse*)output.response;
NSLog(@"put object success!");
}
if (output.error) {
NSLog(@"put object failure");
}
});
[task waitUtilFinished];
}
设定Object的Http Header
BOS支持您在上传object时设定Http Header。
基本流程
- 创建BOSObjectMetadata类的实例。
- 设定BOSObjectMetadata实例的contentEncoding/contentType/contentDisposition等字段。
- 将BOSObjectMetadata实例设置到BOSPutObjectRequest的objectContent.metadata字段上。
示例代码
BOSObjectMetadata* metadata = [[BOSObjectMetadata alloc] init];
metadata.contentEncoding = @"<encoding>";
metadata.contentDisposition = @"<content disposition>";
content.metadata = metadata;
完整示例
#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];
BOSObjectContent* content = [[BOSObjectContent alloc] init];
// 以文件方式
content.objectData.file = @"<file path>";
// 或者以二进制数据方式
NSData* data = [[NSData alloc] init];
content.objectData.data = data;
BOSObjectMetadata* metadata = [[BOSObjectMetadata alloc] init];
metadata.contentEncoding = @"<encoding>";
metadata.contentDisposition = @"<content disposition>";
content.metadata = metadata;
BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
request.bucket = @"<bucketname>";
request.key = @"<Objectname>";
request.objectContent = content;
__block BOSPutObjectResponse* response = nil;
BCETask* task = [client putObject:request];
task.then(^(BCEOutput* output) {
if (output.progress) {
NSLog(@"put object progress is %@", output.progress);
}
if (output.response) {
response = (BOSPutObjectResponse*)output.response;
NSLog(@"put object success!");
}
if (output.error) {
NSLog(@"put object failure");
}
});
[task waitUtilFinished];
}
用户自定义元数据
BOS支持用户自定义元数据来对Object进行描述。
基本流程
- 创建BOSObjectMetadata类的实例。
- 将自定义元数据字典设置到BOSObjectMetadata的userMetadata字段。
示例代码
BOSObjectMetadata* metadata = [[BOSObjectMetadata alloc] init];
metadata.contentEncoding = @"<encoding>";
metadata.contentDisposition = @"<content disposition>";
content.metadata = metadata;
NSDictionary* customMetadata = @{
@"name" : @"my-data"
};
content.metadata.userMetadata = customMetadata;
说明:在上面代码中,用户自定义了一个名字为”name”,值为”my-data”的元数据。当用户下载此Object的时候,此元数据也可以一并得到。一个Object可以有多个类似的参数,但所有的User Meta总大小不能超过2KB。
完整示例
#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];
BOSObjectContent* content = [[BOSObjectContent alloc] init];
// 以文件方式
content.objectData.file = @"<file path>";
// 或者以二进制数据方式
NSData* data = [[NSData alloc] init];
content.objectData.data = data;
BOSObjectMetadata* metadata = [[BOSObjectMetadata alloc] init];
metadata.contentEncoding = @"<encoding>";
metadata.contentDisposition = @"<content disposition>";
content.metadata = metadata;
NSDictionary* customMetadata = @{
@"name" : @"my-data"
};
content.metadata.userMetadata = customMetadata;
BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
request.bucket = @"<bucketname>";
request.key = @"<objectname>";
request.objectContent = content;
__block BOSPutObjectResponse* response = nil;
BCETask* task = [client putObject:request];
task.then(^(BCEOutput* output) {
if (output.progress) {
NSLog(@"put object progress is %@", output.progress);
}
if (output.response) {
response = (BOSPutObjectResponse*)output.response;
NSLog(@"put object success!");
}
if (output.error) {
NSLog(@"put object failure");
}
});
[task waitUtilFinished];
}