Skip to content

feat(storage): add cacheControl header to uploadData, downloadData and getUrl #14410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,48 @@ describe('downloadData with key', () => {
);
});
});

describe('ResponseCacheControl passed in options', () => {
it('should include cacheControl in headers when provided', async () => {
(getObject as jest.Mock).mockResolvedValueOnce({ Body: 'body' });
downloadData({
path: inputKey,
options: {
cacheControl: 'no-store',
},
});

const { job } = mockCreateDownloadTask.mock.calls[0][0];
await job();

expect(getObject).toHaveBeenCalledTimes(1);
await expect(getObject).toBeLastCalledWithConfigAndInput(
expect.any(Object),
expect.objectContaining({
ResponseCacheControl: 'no-store',
}),
);
});

it('should NOT include cacheControl in headers when not provided', async () => {
(getObject as jest.Mock).mockResolvedValueOnce({ Body: 'body' });
downloadData({
path: inputKey,
});

const { job } = mockCreateDownloadTask.mock.calls[0][0];
await job();

expect(getObject).toHaveBeenCalledTimes(1);
await expect(getObject).toBeLastCalledWithConfigAndInput(
expect.any(Object),
{
Bucket: bucket,
Key: 'public/key',
},
);
});
});
});

describe('downloadData with path', () => {
Expand Down Expand Up @@ -544,4 +586,46 @@ describe('downloadData with path', () => {
);
});
});

describe('ResponseCacheControl passed in options', () => {
it('should include cacheControl in headers when provided', async () => {
(getObject as jest.Mock).mockResolvedValueOnce({ Body: 'body' });
downloadData({
path: inputKey,
options: {
cacheControl: 'no-store',
},
});

const { job } = mockCreateDownloadTask.mock.calls[0][0];
await job();

expect(getObject).toHaveBeenCalledTimes(1);
await expect(getObject).toBeLastCalledWithConfigAndInput(
expect.any(Object),
expect.objectContaining({
ResponseCacheControl: 'no-store',
}),
);
});

it('should NOT include cacheControl in headers when not provided', async () => {
(getObject as jest.Mock).mockResolvedValueOnce({ Body: 'body' });
downloadData({
path: inputKey,
});

const { job } = mockCreateDownloadTask.mock.calls[0][0];
await job();

expect(getObject).toHaveBeenCalledTimes(1);
await expect(getObject).toBeLastCalledWithConfigAndInput(
expect.any(Object),
{
Bucket: bucket,
Key: inputPath,
},
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,49 @@ describe('getUrl test with key', () => {
);
});
});

describe('cacheControl passed in options', () => {
it('should include ResponseCacheControl header', async () => {
const cacheControl = 'no-store';
await getUrlWrapper({
key: 'key',
options: {
cacheControl,
},
});
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
await expect(getPresignedGetObjectUrl).toBeLastCalledWithConfigAndInput(
{
credentials,
region,
expiration: expect.any(Number),
},
{
Bucket: bucket,
Key: 'public/key',
ResponseCacheControl: cacheControl,
},
);
});

it('should NOT include ResponseCacheControl header', async () => {
await getUrlWrapper({
key: 'key',
});
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
await expect(getPresignedGetObjectUrl).toBeLastCalledWithConfigAndInput(
{
credentials,
region,
expiration: expect.any(Number),
},
{
Bucket: bucket,
Key: 'public/key',
},
);
});
});
});
describe('Error cases : With key', () => {
afterAll(() => {
Expand Down Expand Up @@ -330,6 +373,51 @@ describe('getUrl test with path', () => {
);
});
});

describe('cacheControl passed in options', () => {
it('should include ResponseCacheControl header', async () => {
const inputPath = 'path/';
const cacheControl = 'no-store';
await getUrlWrapper({
path: inputPath,
options: {
cacheControl,
},
});
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
await expect(getPresignedGetObjectUrl).toBeLastCalledWithConfigAndInput(
{
credentials,
region,
expiration: expect.any(Number),
},
{
Bucket: bucket,
Key: inputPath,
ResponseCacheControl: cacheControl,
},
);
});

it('should not include ResponseCacheControl header', async () => {
const inputPath = 'path/';
await getUrlWrapper({
path: inputPath,
});
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
await expect(getPresignedGetObjectUrl).toBeLastCalledWithConfigAndInput(
{
credentials,
region,
expiration: expect.any(Number),
},
{
Bucket: bucket,
Key: inputPath,
},
);
});
});
});
describe('Happy cases: With path and Content Disposition, Content Type', () => {
const config = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,30 @@ describe('putObjectJob with key', () => {
);
});
});

describe('cacheControl passed in option', () => {
it('should include CacheControl header', async () => {
const job = putObjectJob(
{
path: testPath,
data,
options: {
cacheControl: 'no-store',
},
},
new AbortController().signal,
dataLength,
);
await job();

await expect(mockPutObject).toBeLastCalledWithConfigAndInput(
expect.objectContaining({ credentials, region }),
expect.objectContaining({
CacheControl: 'no-store',
}),
);
});
});
});

describe('putObjectJob with path', () => {
Expand Down Expand Up @@ -454,4 +478,50 @@ describe('putObjectJob with path', () => {
);
});
});

describe('cacheControl passed in option', () => {
it('should include CacheControl header', async () => {
const job = putObjectJob(
{
path: testPath,
data,
options: {
cacheControl: 'no-store',
},
},
new AbortController().signal,
dataLength,
);
await job();

await expect(mockPutObject).toBeLastCalledWithConfigAndInput(
expect.objectContaining({ credentials, region }),
expect.objectContaining({
CacheControl: 'no-store',
}),
);
});

it('should NOT include CacheControl header', async () => {
const job = putObjectJob(
{
path: testPath,
data,
},
new AbortController().signal,
dataLength,
);
await job();

await expect(mockPutObject).toBeLastCalledWithConfigAndInput(
expect.objectContaining({ credentials, region }),
{
Bucket: bucket,
Key: testPath,
Body: data,
ContentType: 'application/octet-stream',
},
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const headObjectHappyCase: ApiFunctionalTestCase<typeof headObject> = [
},
expect.objectContaining({
url: expect.objectContaining({
href: 'https://bucket.s3.us-east-1.amazonaws.com/key',
href: 'https://bucket.s3.us-east-1.amazonaws.com/key?response-cache-control=no-store',
}),
method: 'HEAD',
}),
Expand Down Expand Up @@ -65,7 +65,7 @@ const headObjectHappyCaseCustomEndpoint: ApiFunctionalTestCase<
},
expect.objectContaining({
url: expect.objectContaining({
href: 'https://custom.endpoint.com/bucket/key',
href: 'https://custom.endpoint.com/bucket/key?response-cache-control=no-store',
}),
}),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const downloadDataJob =
...(downloadDataOptions?.bytesRange && {
Range: `bytes=${downloadDataOptions.bytesRange.start}-${downloadDataOptions.bytesRange.end}`,
}),
ResponseCacheControl: downloadDataOptions?.cacheControl,
ExpectedBucketOwner: downloadDataOptions?.expectedBucketOwner,
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const getUrl = async (
...(getUrlOptions?.contentType && {
ResponseContentType: getUrlOptions.contentType,
}),
ResponseCacheControl: getUrlOptions?.cacheControl,
ExpectedBucketOwner: getUrlOptions?.expectedBucketOwner,
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const putObjectJob =
checksumAlgorithm,
onProgress,
expectedBucketOwner,
cacheControl,
} = uploadDataOptions ?? {};

const checksumCRC32 =
Expand Down Expand Up @@ -93,6 +94,7 @@ export const putObjectJob =
ContentType: contentType,
ContentDisposition: constructContentDisposition(contentDisposition),
ContentEncoding: contentEncoding,
CacheControl: cacheControl,
Metadata: metadata,
ContentMD5: contentMD5,
ChecksumCRC32: checksumCRC32,
Expand Down
18 changes: 17 additions & 1 deletion packages/storage/src/providers/s3/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ export type GetUrlOptions = CommonOptions & {
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
*/
contentType?: string;
/**
* The cache-control header value of the file when downloading it.
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
*/
cacheControl?: string;
};

/** @deprecated Use {@link GetUrlWithPathOptions} instead. */
Expand All @@ -192,7 +197,13 @@ export type GetUrlWithPathOptions = GetUrlOptions;
*/
export type DownloadDataOptions = CommonOptions &
TransferOptions &
BytesRangeOptions;
BytesRangeOptions & {
/**
* The cache-control header value of the file when downloading it.
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
*/
cacheControl?: string;
};

/** @deprecated Use {@link DownloadDataWithPathOptions} instead. */
export type DownloadDataWithKeyOptions = ReadOptions & DownloadDataOptions;
Expand Down Expand Up @@ -236,6 +247,11 @@ export type UploadDataOptions = CommonOptions &
* @default undefined
*/
checksumAlgorithm?: UploadDataChecksumAlgorithm;
/**
* The cache-control header value of the file when downloading it.
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
*/
cacheControl?: string;
};

/** @deprecated Use {@link UploadDataWithPathOptions} instead. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type GetObjectInput = Pick<
| 'ResponseContentDisposition'
| 'ResponseContentType'
| 'ExpectedBucketOwner'
| 'ResponseCacheControl'
>;

export type GetObjectOutput = GetObjectCommandOutput;
Expand All @@ -66,6 +67,9 @@ const getObjectSerializer = async (
url.pathname = serializePathnameObjectKey(url, input.Key);
url.search = new AmplifyUrlSearchParams({
'x-id': 'GetObject',
...(input.ResponseCacheControl && {
'response-cache-control': input.ResponseCacheControl,
}),
}).toString();
validateObjectUrl({
bucketName: input.Bucket,
Expand Down
Loading
Loading