All Products
Search
Document Center

Object Storage Service:How do I restore multiple objects at a time?

Last Updated:Apr 30, 2024

If you do not enable real-time access of Archive objects, you can access Archive objects only after you restore them. You can restore only one object each time by using the Object Storage Service (OSS) console. You cannot restore multiple objects in a directory at a time by using the OSS console. To restore multiple objects at a time, you can use other methods. This topic describes how to restore multiple objects at a time.

Solution

To restore multiple objects in a bucket at a time, perform the following steps:

1. List all objects.

  • We recommend that you use the bucket inventory feature to list objects in a bucket. For more information, see Bucket inventory.

  • You can also list objects by calling the ListObjects operation. However, the operation generates higher fees than inventory-based listing. For more information, see List objects and API operation calling fees.

2. If you list objects by using the bucket inventory feature, filter objects whose StorageClass is Archive in the returned inventory list and record the full paths of the objects excluding the bucket name.

Note

If you list objects by using OSS SDKs, use the Key and StorageClass combination to obtain the paths of objects and their storage classes. The following sample code returns the path of the "s" object and its storage class.

System.out.println("fileurl"+s.getKey()+"&stu:"+s.getStorageClass());

3. Restore objects in the corresponding paths at a time. You can use one of the following methods to restore multiple objects at a time. For more information, see Restore objects.

  • ossutil: Compared with the other two methods, ossutil is simpler and more efficient to use, if you are familiar with command line operations.

  • OSS SDKs: Compared with ossutil, using OSS SDKs is more complex and requires the ability to read and modify code. The code is compiled and run on the local device. However, OSS SDKs provide better code reusability. In the later part of this topic, a code sample is provided to show how to restore multiple objects at a time by using OSS SDK for Java.

  • OSS API: The OSS API does not provide an API operation to restore multiple objects at a time. You must call the RestoreObject operation on each of the objects to restore. You must write code to calculate a signature when you call the operation. We recommend that you use the OSS API when your application requires a high level of customization.

SDK example

Additional information

The Java sample code provided in the Restore objects topic restores only a single object. To restore multiple objects at a time, replace the objectName variable in the Java sample code with a variable of the list type that stores the paths of the objects to restore. The following sample code is modified from the Java sample code provided in the Restore objects topic to restore multiple objects at a time.

Note

To check whether the restoration is complete, follow the steps in How do I check whether OSS objects are restored?

If you want to restore Cold Archive or Deep Cold archive objects, see Overview and click the link to the corresponding Restore objects topic.

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;

import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws Exception {
        // In this example, the endpoint of the China (Beijing) region is used. Specify your actual endpoint. 
        String endpoint = "oss-cn-beijing.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";
        // Create a list of the full paths of the Archive objects to restore. Do not include the bucket name in the full paths of the objects. 
        List<String> filePaths = new ArrayList<>();
        filePaths.add("filePath1");
        filePaths.add("filePath2");

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            for (String filePath : filePaths) {
                ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, filePath);
                // Check whether the objects are Archive objects. 
                StorageClass storageClass = objectMetadata.getObjectStorageClass();
                if (storageClass == StorageClass.Archive) {
                    // Restore the objects. 
                    ossClient.restoreObject(bucketName, filePath);
                }
                System.out.println(filePath+" start thawing.");
            }
        } 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();
            }
        }
    }
}

References