All Products
Search
Document Center

Object Storage Service:bucket-versioning

Last Updated:Aug 19, 2024

If you want to be able to restore an object to a previous version after it is accidentally overwritten or deleted, you can run the bucket-versioning command to enable versioning. Object Storage Service (OSS) allows you to configure versioning for a bucket to protect objects in the bucket. After you enable versioning for a bucket, objects in the bucket are stored as previous versions when they are overwritten or deleted.

Usage notes

  • To configure versioning, you must have the oss:PutBucketVersioning permission. To query versioning status, you must have the oss:GetBucketVersioning permission. For more information, see Attach a custom policy to a RAM user.

  • For ossutil 1.6.16 and later, you can directly use ossutil as the binary name in the command line. You do not need to update the binary name based on the operating system. For ossutil earlier than 1.6.16, you need to update the binary name based on the operating system. For more information, see ossutil command reference.

  • For more information about versioning, see Overview.

Configure versioning

  • Command syntax

    ossutil bucket-versioning --method put oss://bucketname versioning

    The following table describes the parameters in the command syntax.

    Parameter

    Description

    bucketname

    The name of the bucket for which you want to configure versioning.

    versioning

    The versioning status of the bucket. Valid values:

    • enabled: enables versioning for the bucket. When an object is uploaded to a versioning-enabled bucket, OSS generates a random string as the globally unique version ID of the object. For more information, see Manage objects in a versioning-enabled bucket.

    • suspended: suspends versioning for the bucket. When an object is uploaded to a versioning-suspended bucket, OSS generates a version ID of null for the uploaded object. For more information, see Manage objects in a versioning-suspended bucket.

    Important

    By default, the versioning status of a bucket is unversioned. After versioning is enabled for a bucket, the versioning status of the bucket cannot be set back to unversioned. However, you can suspend versioning for a versioning-enabled bucket.

  • Examples

    Run the following command to enable versioning for a bucket named examplebucket:

    ossutil bucket-versioning --method put oss://examplebucket enabled

    Run the following command to suspend versioning for a bucket named examplebucket:

    ossutil bucket-versioning --method put oss://examplebucket suspended

    The following sample output indicates that versioning is configured for the bucket:

    0.261209(s) elapsed

Query the versioning status of a bucket

  • Command syntax

    ossutil bucket-versioning --method get oss://bucketname
  • Examples

    Run the following command to query the versioning status of a bucket named examplebucket:

    ossutil bucket-versioning --method get oss://examplebucket

    The following sample output indicates that versioning is enabled for the bucket:

    bucket versioning status:Enabled
    
    0.218001(s) elapsed

    The following sample output indicates that versioning is suspended for the bucket:

    bucket versioning status:Suspended
    
    0.168791(s) elapsed

    The following sample output indicates that versioning status is unversioned:

    bucket versioning status:Null
    
    0.158691(s) elapsed

Related operations

  • An upload to a versioning-enabled bucket behaves in the same way as an upload to an unversioned bucket, except that OSS generates a globally unique version ID for the object that is uploaded to the versioning-enabled bucket. For more information, see Upload objects.

  • After you enable versioning for a bucket, objects that are overwritten or deleted in the bucket are saved as previous versions. You can specify a version ID to download the specified version of an object. For more information, see Download objects. You can specify a version ID to restore the specified previous version of an object. For more information, see Copy objects.

Common options

If you use ossutil to switch to a bucket that is located in another region, add the -e option to specify the endpoint of the region in which the bucket is located. If you use ossutil to switch to a bucket that belongs to another Alibaba Cloud account, add the -i option to specify the AccessKey ID of the specified account, and add the -k option to specify the AccessKey secret of the specified account.

For example, you can run the following command to enable versioning for a bucket named examplebucket, which is located in the China (Hangzhou) region and is owned by another Alibaba Cloud account:

ossutil bucket-versioning--method put oss://examplebucket enabled -e oss-cn-hangzhou.aliyuncs.com -i LTAI4Fw2NbDUCV8zYUzA****  -k 67DLVBkH7EamOjy2W5RVAHUY9H****

For more information about common options, see Common options.