This topic describes how to quickly upload a local file to Object Storage Service (OSS) using simple upload. This method is straightforward and suitable for scenarios where you need to quickly upload a local file.
Considerations
The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region as an example and uses the public endpoint by default. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use the internal endpoint. For more information about the mappings between regions and endpoints supported by OSS, see OSS regions and endpoints.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket Policy.
API | Action | Definition |
PutObject |
| Uploads an object. |
| When uploading an object, if you specify object tags through x-oss-tagging, this permission is required. | |
| When uploading an object, if the object metadata contains X-Oss-Server-Side-Encryption: KMS, these two permissions are required. | |
|
Upload local files
If you upload an object whose name is the same as that of an accessible existing object, the existing object will be overwritten by the uploaded object.
The following table lists the common parameters that you must configure when uploading an object.
Parameter | Description |
bucket | The name of the bucket. Bucket names must comply with the following conventions:
|
key | The full path of the object. Do not include the bucket name in the full path. The object name must comply with the following naming conventions:
|
The following sample code demonstrates how to upload a local file to a bucket:
using System.Text; // Import System.Text namespace for handling character encoding (such as UTF-8 encoded strings)
using OSS = AlibabaCloud.OSS.V2; // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent usage
var region = "cn-hangzhou"; // Required, set the region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou
var bucket = "your bucket name"; // Required, set the target bucket name
var endpoint = null as string; // Optional, specify the endpoint for accessing OSS service. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com
var key = "your object key"; // Required, specify the object name to upload. Format: (folder/objectName)
var filePath = "/Users/yourLocalPath/yourFileName"; // Required, specify the local file path
// Load the default configuration of OSS SDK, which automatically reads credential information (such as AccessKey) from environment variables
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set to use environment variables for credentials, used for identity verification (format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET)
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the bucket region in the configuration
cfg.Region = region;
// If an endpoint is specified, override the default endpoint
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance using the configuration information
using var client = new OSS.Client(cfg);
// Call PutObjectFromFileAsync method to upload a local file
var result = await client.PutObjectFromFileAsync(new()
{
Bucket = bucket,
Key = key
}, filePath);
// Display the upload result
Console.WriteLine("PutObjectFromFile done"); // Indicate the operation is complete
Console.WriteLine($"StatusCode: {result.StatusCode}"); // HTTP status code
Console.WriteLine($"RequestId: {result.RequestId}"); // RequestId, used for troubleshooting by Alibaba Cloud
Console.WriteLine("Response Headers:"); // Response header information
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headersUpload byte arrays
using System.Text; // Import System.Text namespace for handling character encoding (such as UTF-8 encoded strings)
using OSS = AlibabaCloud.OSS.V2; // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent usage
var region = "cn-hangzhou"; // Required, set the region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou
var bucket = "your bucket name"; // Required, set the target bucket name
var endpoint = null as string; // Optional, specify the endpoint for accessing OSS service. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com
var key = "your object key"; // Required, specify the object name to upload. Format: (folder/objectName)
// Load the default configuration of OSS SDK, which automatically reads credential information (such as AccessKey) from environment variables
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set to use environment variables for credentials, used for identity verification (format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET)
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the bucket region in the configuration
cfg.Region = region;
// If an endpoint is specified, override the default endpoint
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance using the configuration information
using var client = new OSS.Client(cfg);
// Content to be uploaded. Example content: simple string "hello oss!", in actual scenarios it could be file streams, byte arrays, etc.
var content = "hello oss!";
// Convert the string to a UTF-8 encoded byte array, then wrap it in a MemoryStream
// MemoryStream is used to process data streams in memory, suitable for small file uploads; for large files, FileStream is recommended
var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(content));
// Call PutObjectAsync method to asynchronously upload an object (requires a request object containing Bucket, Key, and Body)
// This method will upload the data from bodyStream to the specified Key path in the target Bucket
var result = await client.PutObjectAsync(new OSS.Models.PutObjectRequest()
{
Bucket = bucket, // Target bucket name
Key = key, // Unique key of the object in the bucket
Body = bodyStream // Content stream to upload (in this case, string data in memory)
});
// Display the upload result
Console.WriteLine("PutObject done"); // Indicate the operation is complete
Console.WriteLine($"StatusCode: {result.StatusCode}"); // HTTP status code
Console.WriteLine($"RequestId: {result.RequestId}"); // RequestId, used for troubleshooting by Alibaba Cloud
Console.WriteLine("Response Headers:"); // Response header information
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headersReferences
For the complete sample code for simple upload, see putObject.cs.