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

Upload an object from memory or local disks

The following code provides an example on how to upload an object by directly uploading OSSData or by using an NSURL:

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. The full path of the object cannot contain bucket names. 
put.objectKey = @"exampledir/exampleobject.txt";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// put.uploadingData = <NSData *>; // Directly upload NSData. 

// Optional. Set the upload progress. 
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
    // Specify the length of the segment being uploaded, the total length of the segments uploaded, and the total length of the segments to upload. 
    NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
// Configure the optional fields. 
// put.contentType = @"application/octet-stream";
// put.contentMd5 = @"";
// put.contentEncoding = @"";
// put.contentDisposition = @"";
// Configure the object metadata or HTTP headers for the upload. 
// put.objectMeta = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"x-oss-meta-name1", nil]; 
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];

For more information about optional parameters such as contentType, contentMd5, contentEncoding, contentDisposition, and objectMeta, see PutObject.

Upload an object to a directory

Object Storage Service (OSS) does not use a hierarchical structure for objects, but instead uses a flat structure. All elements are stored as objects in buckets. However, OSS supports directories as a concept to group objects and simplify management. To create a directory in the OSS console, you can create an object whose name ends with a forward slash (/). This way, you upload an object 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 an object, the object is uploaded to the folder/subfolder/ directory.
Note The default path is the root directory whose name does not start with a forward slash (/).

Set contentType and enable MD5 verification during an upload

To ensure that the data sent by the client is the same as that received by the OSS server, you can add the Content-MD5 header in the request as object metadata. OSS 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.
Notice Performance may decline when MD5 verification is enabled.

OSS SDKs provide methods to calculate the MD5 hash of the object content encoded in Base64.

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. The full path of the object cannot contain bucket names. 
put.objectKey = @"exampledir/exampleobject.txt";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// put.uploadingData = <NSData *>; // Directly upload NSData. 
// Optional. Set contentType. 
put.contentType = @"application/octet-stream";
// Optional. Set MD5 verification. 
// Set the MD5 hash of the file path. 
put.contentMd5 = [OSSUtil base64Md5ForFilePath:@"<filePath>"]; 
// Set the MD5 hash for the binary data. 
// put.contentMd5 = [OSSUtil base64Md5ForData:<NSData *>];
// Optional. Set the upload progress. 
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
    // Specify the length of the segment being uploaded, the total length of the segments uploaded, and the total length of the segments 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;
}];
// [putTask waitUntilFinished];
// [put cancel];