This topic describes how to upload files from the memory or local disks. You can also perform MD5 verification to ensure data integrity in simple upload.

Upload a file from the memory or local disks

The following code provides an example on how to directly upload NSData or use an NSURL to upload a file:

OSSPutObjectRequest * put = [OSSPutObjectRequest new];

// Specify the bucket name. Example: examplebucket. 
put.bucketName = @"examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. 
put.objectKey = @"exampledir/exampleobject.txt";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// put.uploadingData = <NSData *>; // Directly upload NSData. 

// (Optional) Specify the upload progress. 
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
    // Specify the number of bytes that are being uploaded, the total number of bytes that are uploaded, and the total number of bytes to be uploaded. 
    NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
// Configure the optional fields. 
// put.contentType = @"application/octet-stream";
// Specify Content-MD5. 
// put.contentMd5 = @"eB5eJF1ptWaXm4bijSPyxw==";
// Specify the method that is used to encode the object content. 
// put.contentEncoding = @"identity";
// Specify the method that is used to display the object content. 
// put.contentDisposition = @"attachment";
// Configure the object metadata or HTTP headers for the upload. 
// 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 to overwrite the existing object that has the same name as the file to upload. 
// [meta setObject:@"true" forKey:@"x-oss-forbid-overwrite"];
// Specify tags for the destination object. You can specify multiple tags for the destination object at the same time. 
// [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 when x-oss-server-side-encryption is set 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;
}];
// [putTask waitUntilFinished];
// [put cancel];

Upload a file 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 a file to the directory. The OSS console displays an object whose name ends with a forward slash (/) as a directory.

If you set objectKey to folder/subfolder/file when you upload a file, the file is uploaded to the folder/subfolder/ directory.
Note 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 to the request. The OSS server uses the value of this header to perform MD5 verification. You can explicitly specify the contentType field when you upload a file. If you do not explicitly specify contentType, the SDK automatically determines the content type based on the file name or the uploaded object name.
Notice 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 code provides an example on how to obtain the Base64-encoded MD5 hash of the object content:

OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// Specify the bucket name. Example: examplebucket. 
put.bucketName = @"examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. 
put.objectKey = @"exampledir/exampleobject.txt";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// Directly upload NSData. 
// put.uploadingData = <NSData *>; 
// (Optional) Configure the contentType field. 
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) Specify the upload progress. 
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
    // Specify the number of bytes that are being uploaded, the total number of bytes that are uploaded, and the total number of bytes to be uploaded. 
    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;
}];
// [putTask waitUntilFinished];
// [put cancel];

References

  • For the complete sample code that is used to perform simple upload, visit GitHub.
  • For more information about the API operation that you can call to perform simple upload, see PutObject.