数字海洋:如何将所需的ContentMD5添加到存储策略中,提出了生命周期,CORS或ACL的请求



我将在这里回答自己的问题。我花了几个小时来弄清楚这一点,因为任何地方都没有有关此信息的信息,所以我想我应该在某个地方发布此信息。

我正在使用AWS PHP SDK发送PUT请求,以在我的数字海洋空间中添加生命周期策略,这将不需要,因为它需要ContentMD5标头。这里有两个问题,第一个问题是SDK URL编码路径/键,哪个是/?生命周期,/?位置和/?acl的问题,因为它们成为"/%3flifecycle"。 - 如果这不是您请求路径的一部分,请跳过此段。要暂时停止此添加或更新存储策略,您必须在SDK文件中找到文件RESTSERIALEZER.PHP,如果您使用Composer添加API,它将位于/vendor/aws/aws/aws/aws-sdk-php/src/api/serializer/restSerializer.php在您的作曲家/网站根部,可能在/var/www中。在RestSerializer.php中找到两个RAWURLENCODE函数调用并删除它们,但留下值/参数&quot" RawUrlenCode($ varspecs [$ k]("变成$ varspecs [$ k];

现在,请求将转到正确的URL,生成ContentMd5 您需要根据您的工作来构造一些PHP代码。如果您在文件中将XML文本放入文件中,请使用MD5_FILE(path_to_file_here,true(,如果使用字符串使用MD5(String_here,true(,则使用。然后将其包裹在base64_encode((中,这样它看起来像 base64_encode(md5_file('//path/path/file.xml',true((。最后,使用'contentMd5'=&gt添加到putobject数组中;base64_encode(md5_file('/path/path/file.xml',true((。

php示例与文件:

// $spaceS3Client is a new S3Client object.
// since its a file, I need to get the file first
$xmlfile = fopen('/spaces.xml', 'r+');
$request = $spaceS3Client->putObject([
  'Bucket' => 'myspacename',
  'Key' => '?lifecycle',
  'Body' => $xmlfile,
  'ContentType' => 'application/xml',
  'ContentMD5' => base64_encode(md5_file('/spaces.xml'', true))
]);
// close file
fclose($xmlfile);
// if you are having trouble connecting to your space in the first place with an S3Client object, since its set up for AWS and not DO you need to add an 'endpoint' to the array in new S3Client like 'endpoint' => 'https://'.$myspace.'.'.$myspaceregion.'.digitaloceanspaces.com'. You also need to add 'bucket_endpoint' => true.

这里有两个问题,第一个问题是SDK URL编码路径/键,哪个是/?lifecycle,/?位置和/?ACL的问题"/%3flifecycle" - 如果这不是您请求路径的一部分,请跳过此段。要暂时停止此添加或更新存储策略,您必须在SDK文件中找到文件RESTSERIALEZER.PHP,如果您使用Composer添加API,它将位于/vendor/aws/aws/aws/aws-sdk-php/src/api/serializer/restSerializer.php在您的作曲家/网站根部,可能在/var/www中。在RestSerializer.php中找到两个RAWURLENCODE函数调用并删除它们,但留下值/参数&quot" RawUrlenCode($ varspecs [$ k]("变成$ varspecs [$ k];

现在,请求将转到正确的URL,生成ContentMd5 您需要根据您的工作来构造一些PHP代码。如果您在文件中将XML文本放入文件中,请使用MD5_FILE(path_to_file_here,true(,如果使用字符串使用MD5(String_here,true(,则使用。然后将其包裹在base64_encode((中,这样它看起来像 base64_encode(md5_file('//path/path/file.xml',true((。最后,使用'contentMd5'=&gt添加到putobject数组中;base64_encode(md5_file('/path/path/file.xml',true((。

php示例与文件:

// $spaceS3Client is a new S3Client object.
// since its a file, I need to get the file first
$xmlfile = file_get_contents('/spaces.xml', 'r+');
$request = $spaceS3Client->putObject([
  'Bucket' => 'myspacename',
  'Key' => '?lifecycle',
  'Body' => $xmlfile,
  'ContentType' => 'application/xml',
  'ContentMD5' => base64_encode(md5_file('/spaces.xml', true))
]);
// if you are having trouble connecting to your space in the first place with an S3Client object, since its set up for AWS and not DO you need to add an 'endpoint' to the array in new S3Client like 'endpoint' => 'https://'.$myspace.'.'.$myspaceregion.'.digitaloceanspaces.com'. You also need to add 'bucket_endpoint' => true.
// to check the rules have been set use a getObject request and then use the code below to parse the response.
header('Content-type: text/xml');
$request = $request->toArray()["Body"];
echo $request;

最新更新