AWS Signature Версия 4 - Удалить объект на S3Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 AWS Signature Версия 4 - Удалить объект на S3

Сообщение Anonymous »

Ищу помощь по созданию запроса удаления в PHP / Curl, чтобы удалить объект на AWS S3. Я застрял в этом слишком много часов и не могу заставить это работать. Предоставлено .
https://docs.aws.amazon.com/general/lat ... mlобразно/*
* REFERENCE TO DOCS
* https://docs.aws.amazon.com/AmazonS3/la ... uests.html
* https://docs.aws.amazon.com/AmazonS3/la ... eader.html
*
* PRIMARY DOC
* https://docs.aws.amazon.com/AmazonS3/la ... -auth.html
*/

/*
* access_key secrey_key and the host url are purposely left out or changed
*/

$date_long = gmdate('Ymd\THis\Z');
$date_short = gmdate('Ymd');
$credential = $access_key . '/' . $date_short . '/' . $region . '/s3/aws4_request';
$object_key = '/filename.jpg';

$hashed_payload = strtolower(hash("sha256", "")); // empty hash payload for DELETE request

$request_headers = array(
'host' => 'examplebucket.s3.amazonaws.com',
'x-amz-content-sha256' => $hashed_payload,
'x-amz-date' => $date_long,
);

$signed_headers_string = strtolower(implode(';', array_keys($request_headers)));

/*
* I have tried multiple ways to urlencode this part according to documentation on Create a Cononical Request
* Trying to match the response from AWS Im using only the object key or file pathinfo
* https://docs.aws.amazon.com/AmazonS3/la ... -auth.html
**/

// Build canonical request
$canonical_request = "DELETE\n"; // HTTPRequestMethod
$canonical_request .= $new_object_key . "\n"; // CanonicalURI /filename.jpg
$canonical_request .= "\n"; // CanonicalQueryString no query params

foreach ($request_headers as $header => $value) {
$canonical_request .= strtolower($header) . ':' . trim($value) . "\n"; //CanonicalHeaders
}

$canonical_request .= $signed_headers_string . "\n"; // SignedHeaders
$canonical_request .= $hashed_payload; // HashedPayload

// Build string to sign
$string_to_sign = "AWS4-HMAC-SHA256\n";
$string_to_sign .= $date_long . "\n";
$string_to_sign .= $date_short . '/' . $region. "/s3/aws4_request\n";
$string_to_sign .= hash('sha256', $canonical_request);

// create signature
$sig_key = hash_hmac('sha256', $date_short, 'AWS4' . $secret_key, true);
$sig_key = hash_hmac('sha256', $region, $sig_key, true);
$sig_key = hash_hmac('sha256', 's3', $sig_key, true);
$sig_key = hash_hmac('sha256', 'aws4_request', $sig_key, true);
$signature = hash_hmac('sha256', $string_to_sign, $sig_key);

$curl_headers = array(
'Authorization: AWS4-HMAC-SHA256 Credential='.$credential.',SignedHeaders=host;x-amz-content-sha256;x-amz-date,Signature='.$signature,
);

foreach($request_headers as $header => $value) {
$curl_headers[] = $header.': '.$value;
}

$curl = curl_init();

// I have also tried setting the CURLOPT_URL to examplebucket.s3.amazonaws.com/filename.jpg or directly to the object path
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://examplebucket.s3.amazonaws.com',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_HEADER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_HTTPHEADER => $curl_headers
));
$response = curl_exec($curl);
curl_close($curl);


Подробнее здесь: https://stackoverflow.com/questions/755 ... ject-on-s3
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»