Cette rubrique explique comment répertorier les objets d'un bucket pour lequel la gestion des versions est activée, par exemple en listant tous les objets, les objets dont le nom contient un préfixe spécifié, ainsi que les objets et sous-répertoires d'un répertoire donné.
Notes
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.
Les identifiants d'accès utilisés dans cette rubrique sont obtenus à partir des variables d'environnement. Pour savoir comment configurer ces identifiants, consultez Configurer les identifiants d'accès.
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 Configurer un client (Go SDK V1).
Pour répertorier les objets, vous devez disposer de l'autorisation
oss:ListObjectVersions. Pour plus d'informations, consultez Accorder une politique personnalisée.
Scénarios
La figure suivante présente la structure des répertoires et des objets dans un bucket nommé examplebucket.
examplebucket
└── fun
└── exampleobject.jpg
└── examplefile.txt
└── destfolder
└── image1.jpg
└── image2.png
└── srcfile.txt
└── oss.jpg
Les sections suivantes décrivent comment répertorier différents objets en spécifiant des conditions.
Pour plus d'informations sur les paramètres configurables lors du listage des objets, consultez ListObjectVersions (GetBucketVersions).
Exemple de code
Le code suivant montre comment répertorier les versions de tous les objets, y compris les marqueurs de suppression, dans un bucket spécifié :
package main
import (
"log"
"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 {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// 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 {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the name of the bucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
}
// List the versions of all objects including delete markers in the bucket.
keyMarker := oss.KeyMarker("")
versionIdMarker := oss.VersionIdMarker("")
for {
lor, err := bucket.ListObjectVersions(keyMarker, versionIdMarker)
if err != nil {
log.Fatalf("Failed to list object versions: %v", err)
}
// Display the version IDs of the objects.
for _, objVersion := range lor.ObjectVersions {
log.Printf("Versionid: %s", objVersion.VersionId)
log.Printf("Key: %s", objVersion.Key)
log.Printf("Is Latest: %t", objVersion.IsLatest)
}
// Display the versions of the listed delete markers.
for _, deleteMarker := range lor.ObjectDeleteMarkers {
log.Printf("Delete Marker Versionid: %s", deleteMarker.VersionId)
log.Printf("Delete Marker Key: %s", deleteMarker.Key)
}
// Check whether the required versions are listed. If the versions are incompletely listed, the list operation continues. If the versions are completely listed, the list operation stops.
keyMarker = oss.KeyMarker(lor.NextKeyMarker)
versionIdMarker = oss.VersionIdMarker(lor.NextVersionIdMarker)
if !lor.IsTruncated {
break
}
}
}
Exemple de réponse :
Versionid: CAEQChiBgIDHzNPEthciIDYzZGQ5M2VjOTU2ODRmMzA4ZWM4ODk0NjVmMWEx****
Key: fun/
Is Latest true
Versionid: CAEQChiBgID61tnEthciIDNmOGM2MTQ3ZjU1NTQ2MGFhYWJjNjQxMTQxZWZh****
Key: fun/destfolder/
Is Latest true
Versionid: CAEQChiBgMDczt3EthciIDEwYjliZmQ4MDNiMDQ2Njk4YWMxM2NhM2E5NzQ3****
Key: fun/destfolder/image1.jpg
Is Latest true
Versionid: CAEQChiBgIDszt3EthciIGU5OWU0ZTllMGY3NTRmMmU5NzVjZmJkYmE3ZWYy****
Key: fun/destfolder/image2.png
Is Latest true
Versionid: CAEQChiBgICditzEthciIDBiNTg1ZTZkMDRlMzRjNDdiMjRjMTBlOGUzMTM0****
Key: fun/examplefile.txt
Is Latest true
Versionid: CAEQChiBgMC.itzEthciIDEzNWVlYjNhYzNmMjQ4NWM5Nzc2NzllY2FiYmQ3****
Key: fun/exampleobject.jpg
Is Latest true
Versionid: CAEQChiBgIDMgdbEthciIDUyMGI0NmZlNThkODQwY2ZhNmZhNTQ1Njk4ZTdj****
Key: oss.jpg
Is Latest true
Versionid: CAEQChiBgICIgdbEthciIDdlM2Q1YjYxZDIyZDQyMzI4MTRkNzVmYzdiMTBh****
Key: srcfile.txt
Is Latest true
Scénarios courants
Répertorier les versions des objets dont le nom contient un préfixe spécifié
Répertorier les versions d'un nombre spécifié d'objets
Répertorier les versions de tous les objets par page
Référence
Pour plus d'informations sur l'opération API permettant de répertorier les versions de tous les objets d'un bucket, consultez ListObjectVersions.