This topic describes how to upload objects from the memory or local disks. You can also perform MD5 verification to ensure data integrity in simple upload.
Usage notes
Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.
NoteThe region of the bucket is determined by the endpoint specified for initialization.
To upload an object, you must have the
oss:PutObject
permission. For more information, see Attach a custom policy to a RAM user.
Upload an object from the memory or local disks
The following sample code provides an example on how to directly upload OSSData or use an NSURL to upload an object:
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// Specify the name of the bucket. Example: examplebucket.
put.bucketName = @"examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path.
put.objectKey = @"exampledir/exampleobject.txt";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// put.uploadingData = <NSData *>; // Directly upload NSData.
// (Optional) Configure an upload progress indicator.
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
// Specify the number of bytes that are being uploaded, the number of bytes that are uploaded, and the total number of bytes that you want to upload.
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
// Configure optional fields.
// put.contentType = @"application/octet-stream";
// Specify Content-MD5.
// put.contentMd5 = @"eB5eJF1ptWaXm4bijSPyxw==";
// Specify the method that is used to encode the object.
// put.contentEncoding = @"identity";
// Specify the method that is used to display the object content.
// put.contentDisposition = @"attachment";
// Configure object metadata or HTTP headers.
// NSMutableDictionary *meta = [NSMutableDictionary dictionary];
// Specify the object metadata.
// [meta setObject:@"value" forKey:@"x-oss-meta-name1"];
// Set the access control list (ACL) of the object to private.
// [meta setObject:@"private" forKey:@"x-oss-object-acl"];
// Set the storage class of the object to Standard.
// [meta setObject:@"Standard" forKey:@"x-oss-storage-class"];
// Specify that this upload overwrites an existing object that has the same name.
// [meta setObject:@"true" forKey:@"x-oss-forbid-overwrite"];
// Specify one or more tags for the object.
// [meta setObject:@"a:1" forKey:@"x-oss-tagging"];
// Specify the server-side encryption algorithm that is used to encrypt the destination object when Object Storage Service (OSS) creates the object.
// [meta setObject:@"AES256" forKey:@"x-oss-server-side-encryption"];
// Specify the customer master key (CMK) that is managed by Key Management Service (KMS). This parameter takes effect only if you set x-oss-server-side-encryption to KMS.
// [meta setObject:@"9468da86-3509-4f8d-a61e-6eab1eac****" forKey:@"x-oss-server-side-encryption-key-id"];
// put.objectMeta = meta;
OSSTask * putTask = [client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"upload object success!");
} else {
NSLog(@"upload object failed, error: %@" , task.error);
}
return nil;
}];
// waitUntilFinished blocks execution of the current thread but does not block the task progress.
// [putTask waitUntilFinished];
// [put cancel];
Upload an object to a directory
OSS uses a flat structure instead of a hierarchical structure to store objects. However, OSS allows you to emulate a folder hierarchy. If you want to create a directory in the OSS console, you can create an object whose name ends with a forward slash (/). Then, you can upload an object to the directory. The OSS console displays an object whose name ends with a forward slash (/) as a directory.
For example, if you set objectKey to folder/subfolder/file
, the "file" object is uploaded to the folder/subfolder/
directory.
The default path is the root directory, whose name does not start with a forward slash (/).
Configure contentType and enable MD5 verification during an upload
To ensure that the data received by the OSS server is the same as the data sent by a client, you can add the contentMd5 header in the request. The OSS server uses the value of this header to perform MD5 verification. You can explicitly specify contentType when you upload the object. If you do not explicitly specify contentType, the SDK automatically determines the content type based on the file name or the object name.
Take note that MD5 verification has a negative impact on OSS performance.
The SDK provides the method to obtain the Base64-encoded MD5 hash of the object content. The following sample code provides an example on how to obtain the Base64-encoded MD5 hash of the object content:
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// Specify the name of the bucket. Example: examplebucket.
put.bucketName = @"examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path.
put.objectKey = @"exampledir/exampleobject.txt";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// Directly upload NSData.
// put.uploadingData = <NSData *>;
// (Optional) Configure contentType.
put.contentType = @"application/octet-stream";
// (Optional) Configure MD5 verification.
// Specify the MD5 hash of the file path.
put.contentMd5 = [OSSUtil base64Md5ForFilePath:@"<filePath>"];
// Specify the MD5 hash of the binary data.
// put.contentMd5 = [OSSUtil base64Md5ForData:<NSData *>];
// (Optional) Configure an upload progress indicator.
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
// Specify the number of bytes that are being uploaded, the number of bytes that are uploaded, and the total number of bytes that you want to upload.
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
OSSTask * putTask = [client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"upload object success!");
} else {
NSLog(@"upload object failed, error: %@" , task.error);
}
return nil;
}];
// waitUntilFinished blocks execution of the current thread but does not block the task progress.
// [putTask waitUntilFinished];
// [put cancel];
References
For the complete sample code for simple upload, visit GitHub.
For more information about the API operation that you can call to perform simple upload, see PutObject.
For more information about how to initialize an OSSClient instance, see Initialization.