本文介绍了PHP AWS API原始请求到PUT存储桶生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个网站,该网站具有一项功能,如果用户删除将要存档的图像/视频,我正在使用AWS S3进行存储,并且删除后想在Glacier上移动它,我不想使用AWS开发工具包,因此我正在使用PHP cURL创建Raw请求,从该链接我试图将存储桶生命周期放在对象并完成了一些代码,但是它给我签名不匹配的错误,

I am creating a website, in which there is a feature that if user delete a image/video it will be archived, I am using AWS S3 for storing and on delete want to move it on Glacier, I dont want to use AWS SDK, so i am creating Raw request using PHP cURL, from this link i tried to Put bucket lifecycle on an object, http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html and done some code, but it give me error of mismatch signature,

SignatureDoesNotMatch-我们计算出的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。

这是我的代码,在此我想在 x上应用生命周期。 php 在存储桶中,应用生命周期使其过期,我在做什么错?帮帮我,

This is my code, in this i want to apply lifecycle on x.php which is inside a bucket, apply lifecycle for expire it, What i am doing wrong? Help me,

$AWSaccessKey = 'xxxxxxxxxxxxxxxx';
$AWSsecretKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
$AWSregion = 'xxxxxxxxx';

// bucket
$bucket   = 'xxxxxxxx';
$postdata = $filedata = '<LifecycleConfiguration>
  <Rule>
    <Filter>
      <Prefix>/</Prefix>
    </Filter>
    <Status>Enabled</Status>
    <Expiration>
      <Days>0</Days>
    </Expiration>
  </Rule>
</LifecycleConfiguration>';
$filetype = 'text/plain';
$path     = '/x.php'; // file on which i want to put lifecycle to move it to GLACIER

// file md5
$file_md5 = base64_encode(md5($filedata, true));

// file size
$filesize = strlen($filedata);

// date
$date = gmdate('D, d M Y H:i:s').' +0000';

// -> for putting lifecycle config
$params = array(
    'x-amz-date'          => gmdate('D, d M Y H:i:s \\G\\M\\T'),
);
//'x-amz-security-token'=> $auth['Token']

// sort and stringify params (different to other requests, this is formatted like headers)
$params_str = '';
uksort($params, 'strcmp');
foreach($params as $k=>$v){
    $params_str .= $k.': '.$v."\\n";
}

// -> for putting lifecycle config
$to_sign = "PUT\\n$file_md5\\n$filetype\\n\\n".$params_str.'/'.$bucket.$path;

// create signature
// Note: S3 uses SHA1 instead of 256!
$signature = base64_encode(hash_hmac('SHA1', $to_sign, $AWSsecretKey, true));

$headers = "Host: $bucket.s3.amazonaws.com\\n"; // change to your region
$headers .= $params_str;  // note that the params get added to the header
$headers .= 'Content-MD5: '.$file_md5."\\n";
$headers .= 'Authorization: AWS '.$AWSaccessKey.':'.$signature."\\n";
$headers .= 'Content-Length: '.$filesize."\\n";

$ch = curl_init("http://$bucket.s3-$AWSregion.amazonaws.com");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, explode('\n', $headers));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen(dirname(__FILE__).'/errorlog.txt', 'w'));

$result = curl_exec($ch);
var_dump($result);


推荐答案

我认为您不完全了解生命周期策略

I think you do not fully understand how the lifecycle policy works.

$ path ='/x.php'; //我要放置生命周期的文件将其移动到GLACIER

您不会移动单个文件。您配置一个前缀。
应该在您的XML文档中。
您已经有这个

You do not move individual files. You configure a prefix.that should be in your XML document.You already have this

< Prefix> /< / Prefix>


  1. PUT生命周期应始终为 /?lifecycle 。您将其表示为 /x.php

  2. 使用AWS Signature V4而不是使用V2作为新版本可能会更好区域不支持Signature V2,但是所有区域都支持Signature V4。此处提供更多信息:

  3. 使用Signature V4,即使您看到错误消息 SignatureDoesNotMatch 您还应该看到其他消息,< StringToSignBytes>< / StringToSignBytes> < CanonicalRequest>< / CanonicalRequest> 。这些对于您而言足以隔离和解决此问题。

  4. 您提到要将其移至GLACIER,但在XML内容中并未提及。有关您将需要< StorageClass> GLACIER< / StorageClass> 在您的XML中。

  1. PUT lifecycle should always be to /?lifecycle. And you put it as /x.php
  2. It would probably be better to use AWS Signature V4 as opposed to V2 as some newer regions do not support Signature V2 but all regions support Signature V4. More info here: http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
  3. With Signature V4, even if you do see the error message SignatureDoesNotMatch you should also see other messages, <StringToSignBytes></StringToSignBytes> and <CanonicalRequest></CanonicalRequest>. These should be more than enough for you to isolate and solve this issue.
  4. You mention you want to move it to GLACIER but you do not mention this in your XML content. Refer to "Example 1: Add lifecycle configuration - bucket not versioning-enabled" section on this page for http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html You would need <StorageClass>GLACIER</StorageClass> in your XML.

希望这会有所帮助。

这篇关于PHP AWS API原始请求到PUT存储桶生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 11:18