将缓存策略添加到 Amazon (OVH) S3 存储桶映像

问题描述 投票:0回答:1

我面临与存储在 Amazon (OVH) S3 存储桶中的图像的缓存标头相关的问题。在使用 Google Lighthouse 测试我的网站性能时,我收到了“通过高效的缓存策略提供静态资源”的建议。对于 S3 文件。

为了提供一些背景信息,我使用 OVH 的 S3 存储桶来托管我的网站的图像。当前设置涉及通过 PHP 脚本为这些图像生成预签名 URL。然而,当我分析 Google Lighthouse 的结果时,它指出 S3 图像缺乏缓存规则。

S3 API 中是否有任何配置或参数可以帮助我实现此目标?不幸的是,我在 OVH 的文档中找不到相关信息。由于它是 OVH S3 存储桶,我无法访问 AWS 命令行或任何其他命令行...

我正在联系社区,寻求有关如何有效管理 S3 存储桶中存储的图像的缓存标头的意见或建议。如果有人有优化预签名 URL 缓存的经验,或者知道任何可以以这种方式提供帮助的 S3 API 参数,我将非常感谢您的见解:)

如果需要,这里是我的 php S3 类的摘录,它与 OVH S3 文档中提供的示例非常接近:

  public function getPresignedUrl($s3Key){
        $this->connectToS3() ;
        // le timestamp de l'url signée date d'il y a 6 jours ou plus, il faut redemander une url et mettre à jour la base

        $cmd = $this->s3->getCommand('GetObject', [
            'Bucket' => $this->s3BucketName,
            'Key' => $s3Key
        ]);

        $request = $this->s3->createPresignedRequest($cmd, '+1 week');
        return (string) $request->getUri();
    }

    public function addFileToBucket($filePath, $s3Key = NULL, $generateThumbnail = false, $generateWebp = false){
        $this->connectToS3() ;
        if (empty($s3Key)) {
            $s3Key = "local-file-" . uniqid();
        }
        try {
            $this->s3->putObject([
                'Bucket' =>$this->s3BucketName,
                'Key' => $s3Key,
                'SourceFile' => $filePath
            ]);
            new Log("Ajout du fichier : ".$s3Key." () au bucket", false) ;

            $url = $this->getPresignedUrl($s3Key) ;
            $extension = strtolower(substr(strrchr($filePath, '.'), 1));

            $this->db_file = new db_file($this->page);

            $this->db_file->file_key = $s3Key ;
            $this->db_file->file_last_s3_time = timestampToSql(time()) ;
            $this->db_file->file_url = $url ;
            $this->db_file->file_extension = $extension ;
            $this->db_file->save() ;

            if (in_array($extension,$this->images_extensions) && $generateThumbnail){
                $this->generateThumbnail($s3Key);
            }

            if (in_array($extension,$this->images_extensions) && $extension  != "webp" && $generateWebp){
                $this->generateWebp($s3Key);
            }

        } catch (Exception $exception) {
            new Log("Failed to upload $s3Key with error: " . $exception->getMessage());
        }

    }
amazon-s3 cache-control ovh
1个回答
0
投票

S3 不提供本机缓存机制。

如官方文档所述,您可以使用 CloudFront、ElastiCache 或 MediaStore 等 AWS 服务或使用外部服务来实现存储桶的缓存。

© www.soinside.com 2019 - 2024. All rights reserved.