amazon-s3-php-class icon indicating copy to clipboard operation
amazon-s3-php-class copied to clipboard

restoreObject

Open triwav opened this issue 11 years ago • 2 comments

This class doesn't have a method for restoring an object from glacier so I made one. Feel free to use or modify. One note, besides the code below, you'll also need to change the S3Request class getResponse function.

Change this: if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters) || array_key_exists('website', $this->parameters) || array_key_exists('logging', $this->parameters)) $this->resource .= $query; to this: if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters) || array_key_exists('website', $this->parameters) || array_key_exists('logging', $this->parameters) || array_key_exists('restore', $this->parameters)) $this->resource .= $query;

Sorry if this is in the wrong spot. I couldn't figure out how to do a commit.

/** * Restore object from GLACIER * * @param string $bucket Bucket name * @param string $uri Object URI * @param int $daysToRestoreFor Number of days you want file to stay on S3 storage before it gets deleted keeping only the GLACIER version * @param boolean $returnCode Whether to return response code or a description of the result * @return int | string */ public static function restoreObject($bucket, $uri, $daysToRestoreFor = 30, $returnCode = TRUE){ $responseCodeExplanations = array(409 => '409 RestoreAlreadyInProgress: Object restore is already in progress', 202 => '202 Accepted: file will be restored in the next 3-5 hours', 200 => '200 OK: Object already restored');

    $rest = new S3Request('POST', $bucket, $uri, self::$endpoint);

    $dom = new DOMDocument;
    $restoreRequest = $dom->createElement('RestoreRequest');
    $days = $dom->createElement('Days', $daysToRestoreFor);
    $restoreRequest->appendChild($days);
    $dom->appendChild($restoreRequest);
    $rest->data = $dom->saveXML();

    $rest->setParameter('restore', null);
    $rest->setHeader('Content-Type', 'application/xml');
    $rest->size = strlen($rest->data);

    if ($rest->response->error === false) $rest->getResponse();
    if($rest->response->error === false || $rest->response->code == 409){
        if(array_key_exists($rest->response->code, $responseCodeExplanations)){
            if($returnCode){
                return $rest->response->code;
            }
            else{
                return $responseCodeExplanations[$rest->response->code];
            }
        }
        else if ($rest->response->error === false)
            $rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status');
    }
    else {
        self::__triggerError(sprintf("S3::restoreObject({$bucket}, {$uri}): [%s] %s",
        $rest->response->error['code'], $rest->response->error['message']), __FILE__, __LINE__);
        return false;
    }
}

triwav avatar Apr 10 '13 21:04 triwav

This, along with your getObjectStorageClass function, has been hugely helpful. Thank you.

alimovz avatar Jul 10 '19 02:07 alimovz

thank you

davyzhen avatar Jun 23 '21 14:06 davyzhen