简体   繁体   English

为要上传到AWS S3的图像设置Expires和Cache-Control标头

[英]Setting Expires and Cache-Control headers for images that are being uploaded to AWS S3

I'm using an npm package called node-s3-client which is a high-level wrapper for the aws-sdk for Node.js, to upload a local project directory to an S3 bucket. 我正在使用一个名为node-s3-client的npm软件包,该软件包是Node.js的aws-sdk的高级包装,用于将本地项目目录上载到S3存储桶。

Using that package, I'm passing some metadata to my files, namely key value pairs for Expires and Cache-Control . 使用该程序包,我将一些元数据传递到文件中,即ExpiresCache-Control键值对。 I'm uploading an entire directory which consists of HTML, JS, CSS, JPEG files. 我正在上载包含HTML,JS,CSS,JPEG文件的整个目录。 However when I check my S3 bucket, the headers that I'm setting only applies to JS and CSS files, these headers are not applied to images. 但是,当我检查我的S3存储桶时,我设置的标题仅适用于JS和CSS文件,而这些标题不适用于图像。

I've gone through the documentation of the package and aws-sdk but I can't seem to find what causes the issue of selectively applying my metadata to some files and not applying to others. 我已经阅读了软件包和aws-sdk的文档,但是似乎找不到导致选择性地将我的元数据应用于某些文件而不应用于其他文件的原因。

Here's my config object: 这是我的配置对象:

const s3 = require('node-s3-client')

const s3Config= {
    localDir: './dist',
    deleteRemoved: false,
    s3Params: {
        Bucket: 'cdn',
        Prefix: 'dist/',
        Metadata: {
            'Cache-Control': 'max-age=31536000',
            'Expires': oneYearLater(new Date())
        }
    }
}

const client = s3.createClient({
    s3Options: {
        accessKeyId: KEY_ID,
        secretAccessKey: ACCESS_KEY,
        signatureVersion: 'v4',
        region: 'us-east-2',
        s3DisableBodySigning: true
    }
})

client.uploadDir(s3Config)

What might be causing this issue? 是什么导致此问题?

I think you have an issue on how you set up the params for your object to upload. 我认为您在如何设置对象上传参数方面存在问题。 Try: 尝试:

const s3Config= {
    localDir: './dist',
    deleteRemoved: false,
    s3Params: {
        Bucket: 'cdn',
        Prefix: 'dist/',
        CacheControl: 'max-age=31536000',
        Expires: oneYearLater(new Date())
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM