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

Implement an S3 url parser #4

Open
alexburley opened this issue Nov 18, 2019 · 0 comments
Open

Implement an S3 url parser #4

alexburley opened this issue Nov 18, 2019 · 0 comments

Comments

@alexburley
Copy link
Contributor

I've seen this re-used a few times IE:

// for matching https://s3.eu-west-2.amazonaws.com/tray-csv-file-imports-dev/tests/test_csv.csv
const S3_URL_MATCH = /http(?:s)*:\/\/(?:s3\.(.+)\.)*s3\.([^\.]+)\.amazonaws\.com\/([^\?]*)/;

// for matching https://workflow-file-uploads.s3.us-west-2.amazonaws.com/aaaa or https://workflow-file-uploads.s3-us-west-2.amazonaws.com/aaaa
const S3_URL_MATCH_ALT = /http(?:s)*:\/\/(?:(.+)\.)*s3(?:[-|.](.*))*\.amazonaws\.com\/([^\?]*)/;

// matches one of the URL formats above and return bucket and key
module.exports = url => {
	const s3MatchRes = S3_URL_MATCH.exec(url);
	const s3MatchResAlt = S3_URL_MATCH_ALT.exec(url);

	if (s3MatchRes) {
		const bucketAndKey = s3MatchRes[3];
		const parts = bucketAndKey.split('/');
		const bucket = parts[0];
		parts.shift();
		const key = _.join(parts, '/');

		return {
			region: s3MatchRes[2] ? s3MatchRes[2] : undefined,
			bucket: bucket,
			key: decodeURIComponent(key),
		};
	} else if (s3MatchResAlt) {
		const match = s3MatchResAlt;
		if (!match[1]) {
			return undefined;
		}
		return {
			region: match[2] ? match[2] : undefined,
			bucket: match[1],
			key: decodeURIComponent(match[3]),
		};
	} else {
                
		return error; 
	}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant