我将在这里回答我自己的问题。我花了几个小时才弄清楚这一点,因为任何地方都没有任何信息,所以我认为我应该把它发布在我会首先看的地方。
我正在使用AWS PHP SDK发送一个PUT请求,以将生命周期策略添加到我的Digital Ocean空间,由于它需要ContentMD5标头,因此不需要。这里有两个问题,第一个问题是SDK URL对路径/密钥进行编码,这是/?lifecycle,/?location和/?acl的问题,因为它们变成了“ /%3Flifecycle” –如果跳过,这不是您的请求路径的一部分。要暂时停止此操作以添加或更新存储桶策略,您必须在SDK文件中找到文件RestSerializer.php,如果您使用composer添加了API,则其路径将位于/ vendor / aws / aws-sdk-php / src / Api / Serializer / RestSerializer.php在您的作曲者/网站根目录中,该目录可能位于/ var / www中。在RestSerializer.php中,找到两个rawurlencode函数调用并将其删除,但将值/参数“ 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 / file.xml',true))。最后,使用'ContentMD5'=> base64_encode(md5_file('/ path / file.xml',true))将其添加到putObject数组中。
// $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.
答案 0 :(得分:0)
这里有两个问题,第一个问题是SDK URL编码路径/密钥,这是/?lifecycle,/?location和/?acl的问题,因为它们变成了“ /%3Flifecycle”-跳过如果这不是您的请求路径的一部分,则为本段。要暂时停止此操作以添加或更新存储桶策略,您必须在SDK文件中找到文件RestSerializer.php,如果您使用composer添加了API,则其路径将位于/ vendor / aws / aws-sdk-php / src / Api / Serializer / RestSerializer.php在您的作曲者/网站根目录中,该目录可能位于/ var / www中。在RestSerializer.php中,找到两个rawurlencode函数调用并将其删除,但将值/参数“ 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 / file.xml',true))。最后,使用'ContentMD5'=> base64_encode(md5_file('/ path / file.xml',true))将其添加到putObject数组中。
// $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;