Para configurar reglas de ciclo de vida basadas en la política de último acceso, primero debe habilitar el seguimiento de accesos. Esto le permite supervisar automáticamente los patrones de acceso a los objetos de un bucket de Object Storage Service (OSS) y moverlos a clases de almacenamiento más rentables.
Para obtener más información sobre la API que puede llamar para consultar la hora del último acceso a un objeto, consulte GetObjectMeta.
Notas
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si desea acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región, utilice un endpoint interno. Para más detalles sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.
Las credenciales de acceso se obtienen de variables de entorno. Para saber cómo configurar estas credenciales, consulte Configure access credentials.
Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Configure a client (Go SDK V1).
Habilitar el seguimiento de accesos
A continuación se muestra un ejemplo de código para habilitar el seguimiento de accesos en un bucket llamado 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 el estado del seguimiento de accesos
El siguiente ejemplo de código muestra cómo consultar el estado del seguimiento de accesos de un bucket llamado 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)
}
Referencias
Para obtener más información sobre las reglas de ciclo de vida basadas en el último acceso, consulte Lifecycle rules based on last access time.
Si necesita detalles sobre la API que permite habilitar el seguimiento de accesos, consulte PutBucketAccessMonitor.
Respecto a la API utilizada para consultar el estado del seguimiento de accesos, consulte GetBucketAccessMonitor.