This topic describes how to use the simple upload method to upload local files to OSS. This method provides a straightforward way to quickly upload local files.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
Sample code
The following code shows how to upload a local file to an object named exampleobject.txt in the exampledir folder of the examplebucket bucket.
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual endpoint.
// Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the bucket name, for example, examplebucket.
bucketName := "examplebucket" // Replace the value with the actual bucket name.
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket: %v", err)
}
// Specify the full path of the object, such as exampledir/exampleobject.txt, and the full path of the local file, such as D:\\localpath\\examplefile.txt.
objectKey := "exampledir/exampleobject.txt" // Replace the value with the actual object key.
localFilePath := "D:\\localpath\\examplefile.txt" // Replace the value with the actual local file path.
err = bucket.PutObjectFromFile(objectKey, localFilePath)
if err != nil {
log.Fatalf("Failed to put object from file: %v", err)
}
log.Println("File uploaded successfully.")
}
Common scenarios
References
For the complete sample code for simple upload, see GitHub examples.
For more information about the API operation for simple upload, see PutObject.