This topic describes how to upload an object from memory or upload a local file and enable MD5 verification during simple upload.

Upload an object from memory or upload a local file

You can upload an object by directly uploading OSSData or by using an NSURL:

OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// Configure the required fields. bucketName indicates the name of the bucket. objectKey is equivalent to objectName and indicates the complete path of the object you want to upload to OSS. The path must include the file extension of the object. For example, you can set objectKey to abc/efg/123.jpg.
put.bucketName = @"<bucketName>";
put.objectKey = @"<objectKey>";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filepath>"];
// put.uploadingData = <NSData *>; // Directly upload NSData.
// Configure the optional fields.
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 = @"";
// 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 folder

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 folders as a concept to group objects and simplify management. To create a folder in the OSS console, you can create an object whose name ends with a forward slash (/). The OSS console displays an object whose name ends with a forward slash (/) as a folder.

If you set objectKey to folder/subfolder/file when you upload an object, the object is uploaded to the folder/subfolder/ folder.
Note Note that the default path is the root folder whose name does not start with a forward slash (/).

Set the contentType method and enable MD5 verification during an upload

You can explicitly specify the contentType method when you upload the object. If you do not explicitly specify the contentType method, the SDK automatically determines the content type based on the file name or the object name. Additionally, if the contentMD5 method is specified during object upload, OSS uses the Content-MD5 header value to check whether the received object content is the same as the sent object content. OSS SDK provides methods to calculate the MD5 hash of the object content encoded in Base64.

OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// Configure the required fields.
put.bucketName = @"<bucketName>";
put.objectKey = @"<objectKey>";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filepath>"];
// put.uploadingData = <NSData *>; // Directly upload NSData.
// Optional. Set the contentType method.
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];