2

I am trying to create an archive and retrieval system in php. When the user click on archive button particular files will move to glacier storage from standard storage and when click on restore button file in the glacier storage will retrieved to standard storage.

Using AWS php SDK 3.0 api I have successfully move files to glacier vault and for retrieval an archive-retrieval job is initiated and I got the job Id after 3-5 hours and using that Job id after 5 hours I tried getJobOutput function. And I am getting the response same as mentioned in the api documentation but I am not getting the restored file in my s3 bucket.

Here is my code to upload to glacier and restore from glacier

public function archiveAndRestore() {

$this->s3Client = new S3Client(Configure::read('AWScredentials'));
$this->glacier = GlacierClient::factory(Configure::read('AWScredentials'));

// Upload to glacier
$this->s3Client->registerStreamWrapper();
$context = stream_context_create([
    's3' => ['seekable' => true]
]);
$result = $this->glacier->uploadArchive(array(
    'vaultName' => 'archiveTest',
    'archiveDescription' => 'File Name is archiveTest.txt ',
    'body' => fopen('s3://storage-bucket/Videos/archiveTest.txt', 'r', false, $context),
));

$archiveid = $result->get('archiveId');
$jobId = $this->glacier->initiateJob([
    'accountId' => '-',
    'vaultName' => 'archiveTest',
    'jobParameters' => [
        'Type' => 'archive-retrieval',
        'ArchiveId' => 'ORgyyyqsKwoopp110EvFoyqj3G-csmOKLyy3IJnWF9Dpd8BJfwerEhg241nxHf6y6kNUUyhUHOaY4y8QvWBGESmAopa80f6GZ9C05tyyKANhY-qfBUB6YkfTABg',
    ],
]);   


$this->s3Client->registerStreamWrapper();
$context = stream_context_create([
    's3' => ['seekable' => true]
]);
$stream = fopen('s3://storage-bucket/RetrivedFiles/test1.txt', 'w');  
$result = $this->glacier->getJobOutput([
    'accountId' => '-',
    'jobId' => '2dddfffffff9SwZIOPWxcB7TLm_3apNx--2rIiD7SgjOJjjkrerrcN1YCtivh_zsmpLyczY4br-bhyyX0Ev5B7e6-D1',
    'vaultName' => 'archiveTest',
    'saveAs' => $stream,
]);
fclose($stream);    

}

According to the documentation (aws GetJobOutput operation documentation) saveAs attribute of getJobOutput function is to Specify where the contents of the operation should be downloaded. Can be the path to a file, a resource returned by fopen, or a Guzzle\Http\EntityBodyInterface object. As I am giving a path to the file in s3 also. What will be the issue. Any help is really appreciated. Thanks in advance.

This is the result contained in the response $result which is exactly same as mentioned in documentation

Aws\Result Object ( [data:Aws\Result:private] => Array ( [body] => GuzzleHttp\Psr7\Stream Object ( [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #25 [size:GuzzleHttp\Psr7\Stream:private] => [seekable:GuzzleHttp\Psr7\Stream:private] => 1 [readable:GuzzleHttp\Psr7\Stream:private] => 1 [writable:GuzzleHttp\Psr7\Stream:private] => 1 [uri:GuzzleHttp\Psr7\Stream:private] => php://temp [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array ( ) ) [checksum] => c176c1843fd0c0fc662lh9bb8de916540e6f9dpk9b22020bbb8388jk6f81d1c2 [status] => 200 [contentRange] => [acceptRanges] => bytes [contentType] => application/octet-stream [archiveDescription] => File Name is children-wide.jpg [@metadata] => Array ( [statusCode] => 200 [effectiveUri] => https://glacier.region-name.amazonaws.com/-/vaults/vaultname/jobs/gFdjAl4xhTAVEnmffgfg-Ao3-xmmjghfmqkCLOR1m34gHLQpMd0a3WKCiRRrItv2bklawwZnq9KeIch3LKs8suZoJwk2_/output [headers] => Array ( [x-amzn-requestid] => NzAiVAfrMQbpSjj-2228iiKWK_VteDwNyFTUR7Kyu0duno [x-amz-sha256-tree-hash] => c176c1843khfullc662f09bb8de916540e6f9dcc9b22020bbb8388de6f81d1c2 [accept-ranges] => bytes [x-amz-archive-description] => File Name is children-wide.jpg [content-type] => application/octet-stream [content-length] => 1452770 [date] => Tue, 31 Jan 2017 03:34:26 GMT [connection] => close ) [transferStats] => Array ( [http] => Array ( [0] => Array ( ) ) ) ) ) )

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Senchu Thomas
  • 518
  • 3
  • 9
  • 24
  • Why do you use `$result = ...`, and then never actually examine what `$result` contains? It seems pretty likely that you are discarding valuable information. – Michael - sqlbot Jan 30 '17 at 12:50
  • Hi @Michael-sqlbot I tried without storing value to $result then also the file was not copied to the s3 location. Can you please explain in detail what you mean. I think what I was done may be wrong. Thank you for your valuable answer – Senchu Thomas Jan 30 '17 at 14:40
  • `$result = $this->glacier->getJobOutput ...` What is found in `$result` here? – Michael - sqlbot Jan 30 '17 at 19:35
  • @Michael-sqlbot I have given the output I am getting in $result as an edit. I think that may help you to trace the cause. Thank you.. – Senchu Thomas Jan 31 '17 at 03:42

1 Answers1

2

When you are restoring files from Glacier it will not get Standard as storage class anymore. it will still show Glacier. to determine either files came down from Glacier or not,

Use GetObject instead, and look at the Restore value of the result. And set the Range to "bytes=0-0" to skip retrieving the content of the file itself. And be sure to trap for exceptions

if the object is in Glacier and not restored, AWS will throw an InvalidObjectStateError and the script will die if the error is not caught.

This is what you will see it the item resored.

["Restore"] => string(68)"ongoing-request="false ", expiry-date=" Thu, 12 Oct 2017 00: 00: 00 GMT ""

And this is what you will get if Item is still in Glacier

Fatal error: Uncaught exception 'Aws\S3\Exception\S3Exception' with message 'Error executing "GetObject" on "OBJ PATH"; AWS HTTP error: Client error: GET OBJ PATH resulted in a 403 Forbidden response: InvalidObjectStateThe operation is not valid for the (truncated...) InvalidObjectState (client): The operation is not valid for the object's storage class - InvalidObjectStateThe operation is not valid for the object 's storage class879A42BDC3939282VjgBNmLxhqesAaOnnUKkIahdr9OlUnTPASmjh8zZNVzLeYEDz+QooqoFjyaeoyXGeAa/IPxTBrA=' GuzzleHttp\ Exception\ ClientException: Client error: `GET OBJ PATH in C:\inetpub\wwwroot\cruisecheap.com\php_includes\SDKs\AWS\vendor\aws\aws-sdk-php\src\WrappedHttpHandler.php on line 192

I hope this can help you and other people that having the same problem.

Reza Shek
  • 581
  • 1
  • 8
  • 18