Pour configurer des règles de cycle de vie basées sur la dernière heure d'accès afin de surveiller automatiquement les modèles d'accès aux objets dans un compartiment Object Storage Service (OSS) et de transférer ces objets vers des classes de stockage plus économiques, vous devez d'abord activer le suivi des accès.
Pour plus d'informations sur l'opération API permettant d'interroger l'heure du dernier accès à un objet, consultez GetObjectMeta.
Remarques
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Regions and endpoints.
Dans cette rubrique, les identifiants d'accès sont obtenus à partir de variables d'environnement. Pour savoir comment configurer les identifiants d'accès, consultez Configure access credentials.
Cette rubrique illustre la création d'une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Configure a client (Go SDK V1).
Activer le suivi des accès
L'exemple de code suivant montre comment activer le suivi des accès pour un compartiment nommé 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!")
}
Interroger le statut du suivi des accès
L'exemple de code suivant montre comment interroger le statut du suivi des accès pour un compartiment nommé 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)
}
Références
Pour en savoir plus sur les règles de cycle de vie basées sur la dernière heure d'accès, consultez Lifecycle rules based on last access time.
Pour plus d'informations sur l'opération API permettant d'activer le suivi des accès, consultez PutBucketAccessMonitor.
Pour plus d'informations sur l'opération API permettant d'interroger le statut du suivi des accès, consultez GetBucketAccessMonitor.