Par défaut, l'opération GetObject renvoie uniquement la version actuelle d'un objet issu d'un bucket pour lequel le versioning est activé.
Contexte
Lorsque vous appelez l'opération GetObject sur un bucket, l'un des cas suivants s'applique :
Si la version actuelle de l'objet est un marqueur de suppression, OSS renvoie l'erreur 404 Not Found.
Si vous spécifiez le versionId d'un objet dans le paramètre de requête, la version indiquée est renvoyée. Si vous définissez le versionId sur « null », la version d'objet dont le versionId est nul est renvoyée.
Si vous tentez de récupérer un marqueur de suppression en spécifiant son versionId, OSS renvoie l'erreur 405 Method Not Allowed.
Notes
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 qu'OSS, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez la page Régions et points de terminaison.
Cette rubrique illustre la création d'une instance OSSClient avec un point de terminaison OSS. Pour découvrir d'autres configurations, telles que l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), reportez-vous à la section Initialisation (C# SDK V1).
Autorisations
Par défaut, un compte Alibaba Cloud dispose de toutes les autorisations. Les utilisateurs RAM ou les rôles RAM associés à un compte Alibaba Cloud ne disposent d'aucune autorisation par défaut. Le compte Alibaba Cloud ou l'administrateur du compte doit accorder les autorisations d'opération via des politiques RAM ou une Bucket Policy.
|
API |
Action |
Description |
|
GetObject |
|
Télécharge un objet. |
|
|
Cette autorisation est requise lors du téléchargement d'un objet si vous spécifiez la version de l'objet via versionId. |
|
|
|
Cette autorisation est requise lors du téléchargement d'un objet si les métadonnées de l'objet contiennent X-Oss-Server-Side-Encryption: KMS. |
Exemple de code
L'exemple de code suivant montre comment télécharger un objet :
using System;
using System.IO;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace Samples
{
public class Program
{
public static void Main(string[] args)
{
// Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt.
var objectName = "exampledir/exampleobject.txt";
// Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt.
var downloadFilename = "D:\\localpath\\examplefile.txt";
var versionid = "yourArchiveObjectVersionid";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Download the file to a stream. OssObject contains various information about the file, such as the bucket where the file is stored, the file name, metadata, and an input stream.
var request = new GetObjectRequest(bucketName, objectName)
{
// Specify the version ID of the object.
VersionId = versionid
};
var obj = client.GetObject(request);
using (var requestStream = obj.Content)
{
byte[] buf = new byte[1024];
var fs = File.Open(downloadFilename, FileMode.OpenOrCreate);
var len = 0;
// Read the content of the file to a file or memory using the input stream.
while ((len = requestStream.Read(buf, 0, 1024)) != 0)
{
fs.Write(buf, 0, len);
}
fs.Close();
}
Console.WriteLine("Get object succeeded, vesionid:{0}", versionid);
}
catch (Exception ex)
{
Console.WriteLine("Get object failed. {0}", ex.Message);
}
}
}
}
Références
Pour plus d'informations sur l'opération API utilisée pour télécharger des fichiers, consultez la page GetObject.