Lors du téléchargement d'un objet unique depuis un bucket, vous pouvez spécifier des conditions basées sur la date de dernière modification ou l'ETag. Si les conditions sont remplies, le système télécharge l'objet. Sinon, il renvoie une erreur. Le téléchargement conditionnel réduit les transferts réseau et la consommation de ressources.
Conditions de téléchargement
OSS prend en charge les conditions de téléchargement suivantes.
Vous pouvez utiliser conjointement If-Modified-Since et If-Unmodified-Since. Vous pouvez également utiliser ensemble If-Match et If-None-Match.
Obtenez l'ETag en appelant
ossClient.getObjectMeta.
|
Paramètre |
Description |
|
If-Modified-Since |
Si l'heure spécifiée est antérieure à la date de dernière modification de l'objet, le système télécharge l'objet. Sinon, il renvoie le code 304 Not Modified. |
|
If-Unmodified-Since |
Si l'heure spécifiée est égale ou postérieure à la date de dernière modification de l'objet, le système télécharge l'objet. Sinon, il renvoie le code 412 Precondition Failed. |
|
If-Match |
Si l'ETag spécifié correspond à l'ETag de l'objet, le système télécharge l'objet. Sinon, il renvoie le code 412 Precondition Failed. |
|
If-None-Match |
Si l'ETag spécifié ne correspond pas à l'ETag de l'objet, le système télécharge l'objet. Sinon, il renvoie le code 304 Not Modified. |
Notes d'utilisation
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints pris en charge par OSS, consultez Régions et endpoints.
Dans cette rubrique, les identifiants d'accès sont obtenus à partir des variables d'environnement. Pour plus d'informations, consultez Configuration des 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 avec des identifiants du Security Token Service (STS), consultez Configuration du client.
Le téléchargement conditionnel nécessite l'autorisation
oss:GetObject. Pour plus d'informations, consultez Accorder une politique personnalisée.
Exemples
Le code suivant télécharge un objet sous conditions à l'aide de ossClient.getObject. Vous pouvez également utiliser ossClient.downloadFile de manière similaire.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;
import java.util.Date;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 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.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: testfolder/exampleobject.txt.
String objectName = "testfolder/exampleobject.txt";
String pathName = "D:\\localpath\\examplefile.txt";
// 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.
String region = "cn-hangzhou";
// Create an OSSClient instance.
// Call the shutdown method to release associated resources when the OSSClient is no longer in use.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
// For example, an object was last modified at 13:27:04, September 26, 2023. If the specified time is earlier than the last modified time, such as Tue Sep 25 13:27:04 CST 2023, the object meets the If-Modified-Since condition and the object is downloaded.
request.setModifiedSinceConstraint(new Date("Tue Sep 25 13:27:04 CST 2023"));
// Download the object to your local device.
ossClient.getObject(request, new File(pathName));
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
Références
Pour plus d'informations sur l'opération API permettant d'effectuer un téléchargement conditionnel, consultez GetObject.