douwenan9849 2016-12-19 11:39
浏览 178
已采纳

在AWS s3 php amazon sdk上清除缓存

I add a new media image (using amazon-s3-and-cloudfront and amazon-web-services wordpress plugins) and I need to clear cache of this image.

I use smush PRO to compress image: it compress image only locally so I need to re-put images on S3.

This is my code

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);
    }
}

The variable $new_files contains something like that

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";}

So now i need to clear image.

Someone can help me? I also try https://github.com/subchild/CloudFront-PHP-Invalidator/blob/master/CloudFront.php but it doesn't work.

  • 写回答

1条回答 默认 最新

  • dongxinyue2817 2016-12-19 12:16
    关注

    It seems that your question is not about S3, but about CloudFront.

    You can use AWS SDK for PHP to invalidate any object or objects with createInvalidation: http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.CloudFront.CloudFrontClient.html#_createInvalidation

    If you don't want to use SDK for some reason, here is a good example of plain POST request to invalidate 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.0
    ";
    $msg .= "Host: cloudfront.amazonaws.com
    ";
    $msg .= "Date: {$date}
    ";
    $msg .= "Content-Type: text/xml; charset=UTF-8
    ";
    $msg .= "Authorization: AWS {$access_key}:{$sig}
    ";
    $msg .= "Content-Length: {$len}
    
    ";
    $msg .= $xml;
    
    $fp = fsockopen('ssl://cloudfront.amazonaws.com', 443, 
        $errno, $errstr, 30
    );
    if (!$fp) {
        die("Connection failed: {$errno} {$errstr}
    ");
    }
    fwrite($fp, $msg);
    $resp = '';
    while(! feof($fp)) {
        $resp .= fgets($fp, 1024);
    }
    fclose($fp);
    echo $resp;
    

    Source: https://gist.github.com/claylo/1009169

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系