Skip to content
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

add headers to requests #58

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
46 changes: 41 additions & 5 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,23 @@ export class Client {
*/
async deleteObject(
objectName: string,
options: { bucketName?: string; versionId?: string; governanceBypass?: boolean } = {},
options: {
bucketName?: string;
versionId?: string;
governanceBypass?: boolean;
/**
* Additional headers to include in the request
*/
headers?: Record<string, string>;
} = {},
) {
const bucketName = this.getBucketName(options);
if (!isValidObjectName(objectName)) {
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`);
}

const query: Record<string, string> = options.versionId ? { versionId: options.versionId } : {};
const headers = new Headers();
const headers = new Headers(options.headers ?? {});
if (options.governanceBypass) {
headers.set("X-Amz-Bypass-Governance-Retention", "true");
}
Expand Down Expand Up @@ -441,6 +449,10 @@ export class Client {
bucketName?: string;
versionId?: string;
responseParams?: ResponseOverrideParams;
/**
* Additional headers to include in the request
*/
headers?: Record<string, string>;
},
): Promise<Response> {
return this.getPartialObject(objectName, { ...options, offset: 0, length: 0 });
Expand All @@ -462,6 +474,10 @@ export class Client {
bucketName?: string;
versionId?: string;
responseParams?: ResponseOverrideParams;
/**
* Additional headers to include in the request
*/
headers?: Record<string, string>;
},
): Promise<Response> {
const bucketName = this.getBucketName(options);
Expand All @@ -471,7 +487,8 @@ export class Client {
);
}

const headers = new Headers(Object.entries(options.metadata ?? {}));
const init = { ...options?.headers, ...options?.metadata };
const headers = new Headers(init);
let statusCode = 200; // Expected status code
if (offset || length) {
let range = "";
Expand Down Expand Up @@ -586,6 +603,10 @@ export class Client {
* This will not affect the shape of the result, just its efficiency.
*/
pageSize?: number;
/**
* Additional headers to include in the request
*/
headers?: Record<string, string>;
} = {},
): AsyncGenerator<S3Object, void, undefined> {
for await (const result of this.listObjectsGrouped({ ...options, delimiter: "" })) {
Expand Down Expand Up @@ -617,6 +638,10 @@ export class Client {
* This will not affect the shape of the result, just its efficiency.
*/
pageSize?: number;
/**
* Additional headers to include in the request
*/
headers?: Record<string, string>;
},
): AsyncGenerator<S3Object | CommonPrefix, void, undefined> {
const bucketName = this.getBucketName(options);
Expand Down Expand Up @@ -645,6 +670,7 @@ export class Client {
"max-keys": String(maxKeys),
...(continuationToken ? { "continuation-token": continuationToken } : {}),
},
headers: new Headers(options.headers ?? {}),
returnBody: true,
});
const responseText = await pageResponse.text();
Expand Down Expand Up @@ -719,6 +745,10 @@ export class Client {
* This is a minimum; larger part sizes may be required for large uploads or if the total size is unknown.
*/
partSize?: number;
/**
* Additional headers to include in the request
*/
headers?: Record<string, string>;
},
): Promise<UploadedObjectInfo> {
const bucketName = this.getBucketName(options);
Expand Down Expand Up @@ -797,6 +827,7 @@ export class Client {
objectName,
partSize,
metadata: options?.metadata ?? {},
headers: options?.headers ?? {},
});
// stream => chunker => uploader
await stream.pipeThrough(chunker).pipeTo(uploader);
Expand Down Expand Up @@ -865,7 +896,7 @@ export class Client {
objectName,
query,
// Add custom headers if provided
headers: new Headers(options?.headers),
headers: new Headers(options?.headers ?? {}),
});

const metadata: ObjectMetadata = {};
Expand Down Expand Up @@ -902,6 +933,10 @@ export class Client {
bucketName?: string;
/** Metadata for the new object. If not specified, metadata will be copied from the source. */
metadata?: ObjectMetadata;
/**
* Additional headers to include in the request
*/
headers?: Record<string, string>;
},
): Promise<CopiedObjectInfo> {
const bucketName = this.getBucketName(options);
Expand All @@ -915,7 +950,8 @@ export class Client {
let xAmzCopySource = `${sourceBucketName}/${source.sourceKey}`;
if (source.sourceVersionId) xAmzCopySource += `?versionId=${source.sourceVersionId}`;

const headers = new Headers(options?.metadata);
const init = { ...options?.headers, ...options?.metadata };
const headers = new Headers(init);
if (options?.metadata !== undefined) {
headers.set("x-amz-metadata-directive", "REPLACE");
}
Expand Down
4 changes: 3 additions & 1 deletion object-uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ const multipartTagAlongMetadataKeys = [
export class ObjectUploader extends WritableStream<Uint8Array> {
public readonly getResult: () => UploadedObjectInfo;

constructor({ client, bucketName, objectName, partSize, metadata }: {
constructor({ client, bucketName, objectName, partSize, metadata, headers }: {
client: Client;
bucketName: string;
objectName: string;
partSize: number;
metadata: Record<string, string>;
headers: Record<string, string>;
}) {
let result: UploadedObjectInfo;
let nextPartNumber = 1;
Expand All @@ -53,6 +54,7 @@ export class ObjectUploader extends WritableStream<Uint8Array> {
// Set user metadata as this is not a multipart upload
...metadata,
"Content-Length": String(chunk.length),
...headers,
}),
bucketName,
objectName,
Expand Down