Skip to content

Instantly share code, notes, and snippets.

@axmad386
Created August 1, 2023 08:29
Show Gist options
  • Save axmad386/bd68e934efbadcde4984050f7ce1350c to your computer and use it in GitHub Desktop.
Save axmad386/bd68e934efbadcde4984050f7ce1350c to your computer and use it in GitHub Desktop.
Get S3 public url without s3 sdk
public getUrl(location: string): string {
  const {
    endpoint: configEndpoint,
    bucket,
    forcePathStyle,
    region,
  } = this.$config;
  const awsHost = region ? `s3.${region}.amazonaws.com` : "s3.amazonaws.com";
  let endpoint = (configEndpoint as string) || `https:${awsHost}`;
  endpoint = endpoint.replace(bucket, "");
  const { href, protocol, host } = new URL(endpoint);

  if (href.includes("amazonaws.com")) {
    if (!forcePathStyle) {
      return `${protocol}//${bucket}.${host}/${location}`;
    }
  }
  return `${protocol}//${host}/${bucket}/${location}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment