在AWS S3 PHP Amazon SDK上清除缓存



我添加了一个新的媒体映像(使用 Amazon-S3和Cloudfront Amazon-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/master/cloudfront.php,但不起作用。

看来您的问题不是关于S3,而是关于CloudFront。

您可以将AWS SDK用于PHP,以使任何对象或对象无效createInvalidation:http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-api/class-aws.cloudfront.cloudfront.cloudfront.html.html#_createinvalidation

如果您不想出于某种原因使用SDK,这是普通邮政请求的一个很好的示例,以使CloudFront Cache无效:

<?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.0rn";
$msg .= "Host: cloudfront.amazonaws.comrn";
$msg .= "Date: {$date}rn";
$msg .= "Content-Type: text/xml; charset=UTF-8rn";
$msg .= "Authorization: AWS {$access_key}:{$sig}rn";
$msg .= "Content-Length: {$len}rnrn";
$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

最新更新