Object Storage Service (OSS) propose plusieurs classes de stockage pour couvrir divers scénarios, des données fréquemment consultées aux données rarement accessibles : Standard, Infrequent Access (IA), Archive, Cold Archive et Deep Cold Archive. Dans OSS, le contenu d'un objet est immuable après sa création. Pour modifier la classe de stockage d'un objet, utilisez la méthode Bucket.CopyObject afin de copier l'objet et de créer une nouvelle version avec la classe de stockage souhaitée.
Notes
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous accédez à OSS depuis d'autres services Alibaba Cloud situés dans la même région, privilégiez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.
Les exemples de cette rubrique récupèrent les identifiants d'accès depuis des variables d'environnement. Pour configurer ces identifiants, consultez Configurer les identifiants d'accès à l'aide du SDK OSS pour Python 1.0.
Cette rubrique montre comment créer une instance
OSSClientavec un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Initialisation.Pour modifier la classe de stockage d'un objet, vous devez disposer des autorisations
oss:GetObject,oss:PutObjectetoss:RestoreObject. Pour en savoir plus, consultez Accorder une politique personnalisée.
Exemples
Si vous modifiez la classe de stockage d'un objet IA, Archive, Cold Archive ou Deep Cold Archive, ou si vous supprimez l'objet avant la fin de la durée minimale de stockage, des frais s'appliquent pour la période non écoulée. Pour plus d'informations, consultez Comment suis-je facturé pour les objets dont la durée de stockage est inférieure à la durée minimale ?
Convertir la classe de stockage d'un objet de Standard ou IA vers Archive, Cold Archive ou Deep Cold Archive
L'exemple de code suivant convertit la classe de stockage d'un objet de Standard ou IA vers Archive, Cold Archive ou Deep Cold Archive :
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the bucket region (for example: https://oss-cn-hangzhou.aliyuncs.com).
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
# Make sure that the storage class of the object is Standard or IA.
object_name = 'exampledir/exampleobject.txt'
# Convert the storage class of the object to Archive by setting the x-oss-storage-class header to oss2.BUCKET_STORAGE_CLASS_ARCHIVE.
headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_ARCHIVE}
# Convert the storage class of the object to Cold Archive by setting the x-oss-storage-class header to oss2.BUCKET_STORAGE_CLASS_COLD_ARCHIVE.
# headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_COLD_ARCHIVE}
# Convert the storage class of the object to Deep Cold Archive by setting the x-oss-storage-class header to BUCKET_STORAGE_CLASS_DEEP_COLD_ARCHIVE
# headers = {'x-oss-storage-class': oss2.models.BUCKET_STORAGE_CLASS_DEEP_COLD_ARCHIVE}
# Convert the storage class of the object.
bucket.copy_object(bucket.bucket_name, object_name, object_name, headers)
Convertir la classe de stockage d'un objet d'Archive vers Standard ou IA
Le code ci-dessous convertit la classe de stockage d'un objet d'Archive vers Standard ou IA :
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os
import time
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the bucket region (for example: https://oss-cn-hangzhou.aliyuncs.com).
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
# Make sure that the storage class of the object is Archive.
object_name = 'exampledir/exampleobject.txt'
# Obtain the object metadata.
meta = bucket.head_object(object_name)
# Restore the object. The amount of time required to restore an object varies based on the size of the object.
if meta.resp.headers['x-oss-storage-class'] == oss2.BUCKET_STORAGE_CLASS_ARCHIVE:
bucket.restore_object(object_name)
while True:
meta = bucket.head_object(object_name)
if meta.resp.headers['x-oss-restore'] == 'ongoing-request="true"':
time.sleep(5)
else:
break
# Convert the storage class of the object to Standard by setting the x-oss-storage-class header to oss2.BUCKET_STORAGE_CLASS_STANDARD.
headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_STANDARD}
# Convert the storage class of the object to IA by setting the x-oss-storage-class header to oss2.BUCKET_STORAGE_CLASS_IA.
# headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_IA}
# Convert the storage class of the object.
bucket.copy_object(bucket.bucket_name, object_name, object_name, headers)
Convertir la classe de stockage d'un objet de Cold Archive vers Standard ou IA
L'exemple suivant convertit la classe de stockage d'un objet de Cold Archive vers Standard ou IA :
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os
import time
from oss2.models import RESTORE_TIER_EXPEDITED, RestoreJobParameters
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the bucket region (for example: https://oss-cn-hangzhou.aliyuncs.com).
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
# Make sure that the storage class of the object is Cold Archive.
object_name = 'exampledir/exampleobject.txt'
# Obtain the object metadata.
meta = bucket.head_object(object_name)
# Restore the object.
if meta.resp.headers['x-oss-storage-class'] == oss2.BUCKET_STORAGE_CLASS_COLD_ARCHIVE:
# Specify the restoration priority. RESTORE_TIER_EXPEDITED specifies that the object is restored within 1 hour.
job_parameters = RestoreJobParameters(RESTORE_TIER_EXPEDITED)
# Specify the duration for which the object can remain in the restored state. Unit: days.
restore_config = oss2.models.RestoreConfiguration(days=5, job_parameters=job_parameters)
bucket.restore_object(object_name, input=restore_config)
while True:
meta = bucket.head_object(object_name)
if meta.resp.headers['x-oss-restore'] == 'ongoing-request="true"':
time.sleep(5)
else:
break
# Convert the storage class of the object to Standard by setting the x-oss-storage-class header to oss2.BUCKET_STORAGE_CLASS_STANDARD.
headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_STANDARD}
# Convert the storage class of the object to IA by setting the x-oss-storage-class header to oss2.BUCKET_STORAGE_CLASS_IA.
# headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_IA}
# Convert the storage class of the object.
bucket.copy_object(bucket.bucket_name, object_name, object_name, headers)
Références
Pour plus d'informations sur l'opération API permettant de modifier la classe de stockage d'un objet, consultez CopyObject.