最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting Expires and Cache-Control headers for images that are being uploaded to AWS S3 - Stack Overflow

programmeradmin0浏览0评论

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.

Using that package, I'm passing some metadata to my files, namely key value pairs for Expires and Cache-Control. I'm uploading an entire directory which consists of HTML, JS, CSS, JPEG files. 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.

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.

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'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.

Using that package, I'm passing some metadata to my files, namely key value pairs for Expires and Cache-Control. I'm uploading an entire directory which consists of HTML, JS, CSS, JPEG files. 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.

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.

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?

Share Improve this question edited Aug 23, 2017 at 19:53 cinnaroll45 asked Aug 23, 2017 at 16:32 cinnaroll45cinnaroll45 2,8007 gold badges26 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论