De forma predeterminada, la operación GetObject solo devuelve la versión actual de un objeto en un bucket con control de versiones habilitado.
Contexto
Cuando se llama a la operación GetObject en un bucket, se aplica uno de los siguientes casos:
Si la versión actual del objeto es una marca de eliminación, OSS devuelve 404 Not Found.
Si especifica el versionId de un objeto en el parámetro de consulta, se devuelve la versión indicada. Si establece el versionId como "null", se devuelve la versión del objeto que tiene un versionId nulo.
Si intenta recuperar una marca de eliminación al especificar su versionId, OSS devuelve 405 Method Not Allowed.
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 en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.
Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para conocer configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Create an OssClient instance.
Permisos
De forma predeterminada, una cuenta de Alibaba Cloud tiene todos los permisos. Los usuarios o roles de RAM dentro de una cuenta de Alibaba Cloud no cuentan con ningún permiso de manera predeterminada. La cuenta de Alibaba Cloud o el administrador de la cuenta debe otorgar los permisos de operación mediante RAM policies o Bucket Policy.
|
API |
Action |
Descripción |
|
GetObject |
|
Descarga un objeto. |
|
|
Este permiso es obligatorio cuando se descarga un objeto y se especifica su versión mediante versionId. |
|
|
|
Este permiso es obligatorio cuando se descarga un objeto cuyos metadatos contienen X-Oss-Server-Side-Encryption: KMS. |
Código de ejemplo
A continuación se muestra el código para descargar un archivo:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* 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. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, such as examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path cannot contain the bucket name, such as exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Download the file to the local memory. */
GetObjectRequest request(BucketName, ObjectName);
request.setVersionId("yourObjectVersionId");
auto outcome = client.GetObject(request);
if (outcome.isSuccess()) {
std::cout << "getObjectToBuffer" << " success, Content-Length:" << outcome.result().Metadata().ContentLength() << std::endl;
/* Read the content of the downloaded object. */
std::string content;
*(outcome.result().Content()) >> content;
std::cout << "getObjectToBuffer" << "content:" << content << std::endl;
/* View the version ID of the downloaded object. */
std::cout << "versionid:" << outcome.result().VersionId() << std::endl;
}
else {
/* Handle the exception. */
std::cout << "getObjectToBuffer fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Referencias
Para obtener más información sobre la operación de API utilizada para descargar un archivo, consulte GetObject.