OSSClient serves as the Go client used to manage Object Storage Service (OSS) resources such as buckets and objects. To use OSS SDK for Go to initiate a request, you must initialize an OSSClient instance and modify the default configuration items based on your business requirements.
Prerequisite
Access credentials are configured. For more information, see configure access credentials.
Create an OSSClient instance
In the following example, an OSSClient instance is created by loading the default configurations.
package main
import (
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
func main() {
var (
// In this example, the China (Hangzhou) region is used.
region = "cn-hangzhou"
// In this example, access credentials are obtained from environment variables.
provider credentials.CredentialsProvider = credentials.NewEnvironmentVariableCredentialsProvider()
)
// Specify the parameters of an OSS client.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(provider).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
}
You can specify a region to which you want the request to be sent, such as cn-hangzhou or cn-shanghai. For more information about the supported regions, see OSS regions and endpoints.
OSS SDK for Go does not have a default region. You must specify the config.WithRegion parameter to explicitly specify a region when you load the configurations.
Example:
cfg := oss.LoadDefaultConfig().WithRegion("cn-hangzhou")
OSS SDK for Go V2 uses the V4 signature algorithm by default. In this case, you must specify the region parameter.
Configure an OSSClient instance
In most cases, the default OSSClient that uses the default configurations can meet the business requirements. You can also modify the default configurations of OSSClient or use a custom HTTP Client to meet the needs of specific environments.
Common configurations
You can use config to modify common configurations as shown in the following sample code.
cfg := oss.LoadDefaultConfig().
WithConnectTimeout(10 * time.Second).
UploadBandwidthlimit(10*1024)
Common configuration parameters
Region | (Required) The region to which requests are sent. | WithRegion("cn-hangzhou") |
CredentialsProvider | (Required) The access credentials. | WithCredentialsProvider(provider) |
Endpoint | The endpoint. | WithEndpoint("oss-cn-hanghzou.aliyuncs.com") |
HttpClient | The HTTP client. | WithHttpClient(customClient) |
RetryMaxAttempts | The maximum number of HTTP retries. Default value: 3. | WithRetryMaxAttempts(5) |
Retryer | The retry configurations for HTTP requests. | WithRetryer(customRetryer) |
ConnectTimeout | The timeout period for establishing a connection. Default value: 5. Unit: seconds. | WithConnectTimeout(10 * time.Second) |
ReadWriteTimeout | The timeout period for the application to read and write data. Default value: 10. Unit: seconds. | WithReadWriteTimeout(30 * time.Second) |
InsecureSkipVerify | Specifies whether to skip SSL certificate verification. By default, the SSL certificates are verified. | WithInsecureSkipVerify(true) |
EnabledRedirect | Specifies whether to enable HTTP redirection. By default, HTTP redirection is disabled. | WithEnabledRedirect(true) |
ProxyHost | Specifies a proxy server. | WithProxyHost("http://user:passswd@proxy.example-***.com") |
ProxyFromEnvironment | Specifies a proxy server by using environment variables. | WithProxyFromEnvironment(true) |
UploadBandwidthlimit | The upper limit for the total upload bandwidth. Unit: KiB/s. | WithUploadBandwidthlimit(10*1024) |
DownloadBandwidthlimit | The upper limit for the total download bandwidth. Unit: KiB/s. | WithDownloadBandwidthlimit(10*1024) |
SignatureVersion | The version of signature. Default value: v4 | WithSignatureVersion(oss.SignatureVersionV1) |
LogLevel | Specifies the log level. | WithLogLevel(oss.LogInfo) |
LogPrinter | The log printing operation. | WithLogPrinter(customPrinter) |
DisableSSL | Specifies that HTTPS is not used for requests. HTTPS is used by default. | WithDisableSSL(true) |
UsePathStyle | The path request style, which is also known as the root domain name request style. By default, the default domain name of the bucket is used. | WithUsePathStyle(true) |
UseCName | Specifies whether to use a custom domain name to access OSS. By default, a custom domain name is not used. | WithUseCName(true) |
UseDualStackEndpoint | Specifies whether to use a dual-stack endpoint to access OSS. By default, a dual-stack endpoint is not used. | WithUseDualStackEndpoint(true) |
UseAccelerateEndpoint | Specifies whether to use an OSS-accelerated endpoint to access OSS. By default, an OSS-accelerated endpoint is not used. | WithUseAccelerateEndpoint(true) |
UseInternalEndpoint | Specifies whether to use an internal endpoint to access OSS. By default, an internal endpoint is not used. | WithUseInternalEndpoint(true) |
DisableUploadCRC64Check | Specifies that CRC-64 is disabled during object upload. By default, CRC-64 is enabled. | WithDisableUploadCRC64Check(true) |
DisableDownloadCRC64Check | Specifies that CRC-64 is disabled during object download. By default, CRC-64 is enabled. | WithDisableDownloadCRC64Check(true) |
AdditionalHeaders | Specifies additional signed request headers, which is valid if the V4 signature algorithm is used. | WithAdditionalHeaders([]string{"content-length"}) |
UserAgent | Specifies additional User-Agent information. | WithUserAgent("user identifier") |
Customize the OSSClient
If common parameters cannot meet your requirements, you can use WithHTTPClient to replace the default HTTP client.
package main
import (
"crypto/tls"
"net/http"
"time"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/transport"
)
func main() {
var (
// In this example, the China (Hangzhou) region is used.
region = "cn-hangzhou"
// In this example, access credentials are obtained from environment variables.
provider credentials.CredentialsProvider = credentials.NewEnvironmentVariableCredentialsProvider()
)
// Specify common timeout period or other parameters.
transConfig := transport.Config{
// Specify the timeout period for a connection. Default value: 5. Unit: seconds.
ConnectTimeout: oss.Ptr(10 * time.Second),
// Specify the timeout period for the application to read and write data. Default value: 10. Unit: seconds.
ReadWriteTimeout: oss.Ptr(20 * time.Second),
// Specify the timeout period for an idle connection. Default value: 50. Unit: seconds.
IdleConnectionTimeout: oss.Ptr(40 * time.Second),
// Specify the retention period of a network connection. Default value: 30. Unit: seconds.
KeepAliveTimeout: oss.Ptr(40 * time.Second),
// Specify whether to enable HTTP redirection. By default, HTTP redirection is disabled.
EnabledRedirect: oss.Ptr(true),
}
// Specify http.Transport parameters.
var transports []func(*http.Transport)
// Specify the maximum number of connections. Default value: 100.
transports = append(transports, transport.MaxConnections(200))
// If a request contains the Expect: 100-Continue header, it indicates the maximum period of time to wait for the first response header returned from the server after request headers are completely written. Default value: 1. Unit: seconds.
transports = append(transports, transport.ExpectContinueTimeout(2*time.Second))
// Specify the earliest version of Transport Layer Security (TLS). Default value: TLS 1.2.
transports = append(transports, transport.TLSMinVersion(tls.VersionTLS13))
// Specify whether to skip the SSL certificate verification. By default, the SSL certificate is verified.
transports = append(transports, transport.InsecureSkipVerify(true))
// For more information about configuring the Transport parameter, see https://pkg.go.dev/net/http#Transport
// Create a custom HTTP Client.
customClient := transport.NewHttpClient(&transConfig, transports...)
cfg := oss.LoadDefaultConfig().
WithHttpClient(customClient).
WithCredentialsProvider(provider).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
}
Reference
For more information, see developer guide.