Skip to content

🎥 Make videos programmatically with React

759 B
Raw Blame
import {GetBucketLocationCommand} from '@aws-sdk/client-s3';
import type {BucketExists} from './aws-operations';
import {getS3Client} from './get-s3-client';

export const bucketExistsInRegionImplementation: BucketExists = async ({
	bucketName,
	region,
	expectedBucketOwner,
	forcePathStyle,
	requestHandler,
}) => {
	try {
		const bucket = await getS3Client({
			region,
			customCredentials: null,
			forcePathStyle,
			requestHandler,
		}).send(
			new GetBucketLocationCommand({
				Bucket: bucketName,
				ExpectedBucketOwner: expectedBucketOwner ?? undefined,
			}),
		);

		return (bucket.LocationConstraint ?? 'us-east-1') === region;
	} catch (err) {
		if ((err as {Code: string}).Code === 'NoSuchBucket') {
			return false;
		}

		throw err;
	}
};