簡體   English   中英

清除AWS s3 php亞馬遜sdk上的緩存

[英]clear cache on AWS s3 php amazon sdk

我添加了一個新的媒體映像(使用amazon-s3-and-cloudfrontamazon-web-services wordpress插件),並且需要清除該映像的緩存。

我使用smush PRO壓縮圖像:它僅在本地壓縮圖像,因此我需要在S3上重新放置圖像。

這是我的代碼

global $as3cf;
if ( ! $as3cf instanceof Amazon_S3_And_CloudFront ) return;

$results = new WP_Query( $query );

$attachments=(array)$results->get_posts();
if(!empty($attachments)){
    foreach($attachments as $attachment){
        $amazons3_info=get_post_meta($attachment->ID,'amazonS3_info');
        @$as3cf->delete_attachment($attachment->ID);
        $new_files = $as3cf->upload_attachment_to_s3($attachment->ID);
        if(is_wp_error($new_files) && isset($amazons3_info) && !empty($amazons3_info)){
            update_post_meta($attachment->ID,'amazonS3_info',$amazons3_info);
        }
        update_post_meta($attachment->ID,'my-smpro-smush',$new_files);
    }
}

變量$ new_files包含類似的內容

a:3:{s:6:"bucket";s:21:"static.example.com";s:3:"key";s:63:"wp-content/uploads/2016/12/334ca0545d748d0fe135eb30212154db.jpg";s:6:"region";s:9:"eu-west-1";}

所以現在我需要清除圖像。

有人可以幫我嗎? 我也嘗試使用https://github.com/subchild/CloudFront-PHP-Invalidator/blob/master/CloudFront.php,但這不起作用。

看來您的問題不是關於S3,而是關於CloudFront。

您可以使用適用於PHP的AWS開發工具包通過createInvalidation使任何一個或多個對象無效: http : //docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.CloudFront.CloudFrontClient.html#_createInvalidation

如果由於某種原因不想使用SDK,下面是一個簡單的POST請求示例,該示例使CloudFront緩存無效:

<?php
/**
 * Super-simple AWS CloudFront Invalidation Script
 * 
 * Steps:
 * 1. Set your AWS access_key
 * 2. Set your AWS secret_key
 * 3. Set your CloudFront Distribution ID
 * 4. Define the batch of paths to invalidate
 * 5. Run it on the command-line with: php cf-invalidate.php
 * 
 * The author disclaims copyright to this source code.
 *
 * Details on what's happening here are in the CloudFront docs:
 * http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html
 * 
 */
$access_key = 'AWS_ACCESS_KEY';
$secret_key = 'AWS_SECRET_KEY';
$distribution = 'DISTRIBUTION_ID';
$epoch = date('U');

$xml = <<<EOD
<InvalidationBatch>
    <Path>/index.html</Path>
    <Path>/blog/index.html</Path>
    <CallerReference>{$distribution}{$epoch}</CallerReference>
</InvalidationBatch>
EOD;


/**
 * You probably don't need to change anything below here.
 */
$len = strlen($xml);
$date = gmdate('D, d M Y G:i:s T');
$sig = base64_encode(
    hash_hmac('sha1', $date, $secret_key, true)
);

$msg = "POST /2010-11-01/distribution/{$distribution}/invalidation HTTP/1.0\r\n";
$msg .= "Host: cloudfront.amazonaws.com\r\n";
$msg .= "Date: {$date}\r\n";
$msg .= "Content-Type: text/xml; charset=UTF-8\r\n";
$msg .= "Authorization: AWS {$access_key}:{$sig}\r\n";
$msg .= "Content-Length: {$len}\r\n\r\n";
$msg .= $xml;

$fp = fsockopen('ssl://cloudfront.amazonaws.com', 443, 
    $errno, $errstr, 30
);
if (!$fp) {
    die("Connection failed: {$errno} {$errstr}\n");
}
fwrite($fp, $msg);
$resp = '';
while(! feof($fp)) {
    $resp .= fgets($fp, 1024);
}
fclose($fp);
echo $resp;

資料來源: https : //gist.github.com/claylo/1009169

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM