简体   繁体   中英

Get Glacier file status in AWS .NET SDK

I am using the Amazon .NET SDK in an application that needs to be able to download a batch of files from S3. Some of those files may have been archived to Glacier, in which case the application should initiate RestoreObjectRequests for any such files and not download any files from S3. Here are some solutions that don't work:

  • The S3Object from a ListObjectsResponse has a StorageClass property that is S3StorageClass.Glacier for Glacier-enabled files. However, the S3Object continues to have that setting even while the file is temporarily restored, so that doesn't help.

  • I had hoped that a RestoreObjectResponse would return some different result if the file in question was either in the middle of a restore or was temporarily restored, but it continues to return 0 / OK.

  • The only way that I can find of determining whether or not a file is currently available is by attempting a GetObjectRequest and seeing if it fails. I don't want to use that solution, since it could involve downloading a large number of files only to find that one of them is in Glacier.

Can anyone suggest another option that would enable to me to know if all of the images are available without needing to download them? Thanks!

OK, I have a solution. Rather than pulling a full file from S3, it is possible to pull just the first byte:

GetObjectRequest getObjectRequest = new GetObjectRequest();
getObjectRequest.BucketName = bucketName;
getObjectRequest.Key = key;
getObjectRequest.ByteRange = new ByteRange(0, 1);

try
{
    s3Client.GetObject(getObjectRequest);
}
catch (AmazonS3Exception ex)
{
    if (ex.ErrorCode == "InvalidObjectState")
    {
        // In Glacier, perform appropriate actions
    }
    else throw ex;
}

// If no exception, object has been restored

I still think that it would have been better for them to provide something in the Response object that let you know the status rather than throwing an exception, but this method will at least work for what I need.

you can do a GetObjectMetadata and the response class contains 2 related properties:

  1. RestoreInProgress - if false then restore is complete/failed.
  2. RestoreExpiration - gets the expiration date of the restored file (if restored).

you can try something like this:

    GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest
    {
        BucketName = bucketName,
        Key = objectKey
    };
    GetObjectMetadataResponse response = await client.GetObjectMetadataAsync(metadataRequest);
    if (!response.RestoreInProgress)
    {
        return (response.RestoreExpiration.HasValue && response.RestoreExpiration.Value<DateTime.UtcNow);
    }
    return false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM