Para configurar regras de ciclo de vida com base na política de último acesso, monitorar automaticamente os padrões de acesso aos objetos em um bucket do Object Storage Service (OSS) e mover os objetos para classes de armazenamento mais econômicas, ative primeiro o rastreamento de acesso.
Para obter mais informações sobre a operação de API usada para consultar o horário em que um objeto foi acessado pela última vez, consulte GetObjectMeta.
Observações
Este tópico utiliza o endpoint público da região China (Hangzhou). Caso precise acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre as regiões e os endpoints do OSS, consulte Regions and endpoints.
Neste tópico, o código obtém as credenciais de acesso a partir de variáveis de ambiente. Para obter mais informações sobre como configurar as credenciais de acesso, consulte Configure access credentials.
Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar com credenciais do Security Token Service (STS), consulte Configure a client (Go SDK V1).
Ativar o rastreamento de acesso
O código de exemplo a seguir mostra como ativar o rastreamento de acesso para um bucket chamado examplebucket:
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
/// Obtain access credentials from environment variables. Before you run the 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 {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of the bucket.
bucketName := "examplebucket"
// Enable access tracking for examplebucket.
access := oss.PutBucketAccessMonitor{
Status: "Enabled",
}
err = client.PutBucketAccessMonitor(bucketName, access)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("put bucket access monitor success!")
}
Consultar o status do rastreamento de acesso
O código de exemplo a seguir mostra como consultar o status do rastreamento de acesso de um bucket chamado examplebucket:
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
/// Obtain access credentials from environment variables. Before you run the 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 {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of the bucket.
bucketName := "examplebucket"
// View the access tracking status of examplebucket.
result, err := client.GetBucketAccessMonitor(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("bucket access monitor config is:%s\n", result.Status)
}
Referências
Para obter mais informações sobre as regras de ciclo de vida com base no último acesso, consulte Lifecycle rules based on last access time.
Para obter mais informações sobre a operação de API usada para ativar o rastreamento de acesso, consulte PutBucketAccessMonitor.
Para obter mais informações sobre a operação de API usada para consultar o status do rastreamento de acesso, consulte GetBucketAccessMonitor.