简体   繁体   English

使用正则表达式从 aws s3 url 中提取存储桶名称

[英]Extract bucket name from aws s3 url using regex

I want to extract the bucket name from AWS s3 URLs.我想从 AWS s3 URL 中提取存储桶名称。

URLs can be in many formats. URL 可以有多种格式。 Below is the list of regex for supported s3 URLs:以下是支持的 s3 URL 的正则表达式列表:

[a-z0-9.-]+\.s3\.amazonaws\.com
[a-z0-9.-]+\.s3-[a-z0-9-]+\.amazonaws\.com
[a-z0-9.-]+\.s3\.[a-z0-9-]+\.amazonaws\.com
[a-z0-9.-]+\.s3-website[.-](eu|ap|us|ca|sa|cn)

Example:例子:

bucket-name.s3.us-west-2.amazonaws.com
bucket.name.s3.us-west-2.amazonaws.com
bucket-name.s3-us-west-2.amazonaws.com
bucket.name.s3-us-west-2.amazonaws.com
bucket-name.s3.amazonaws.com
bucket.name.s3.amazonaws.com

I want a single regex that can extract bucket-name from these URLs in GoLang .我想要一个可以从GoLang中的这些 URL 中提取bucket-name的正则表达式。

This would work:这会起作用:

^(.+)(?:\.s3[-.].*)$

Translated:翻译:

From the beginning of the string find everything leading up to .s3.从字符串的开头找到通向.s3. or .s3- and capture it into group #1..s3-并将其捕获到第 1 组中。

Your bucket name will be in $1 .您的存储桶名称将在$1中。

See the regex101 link below and use the code generator to see a Golang example.请参阅下面的 regex101 链接并使用代码生成器查看 Golang 示例。

https://regex101.com/r/LRvA5F/1 https://regex101.com/r/LRvA5F/1

Use采用

^(.*?)\.s3\b

See proof .证明

Explanation解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  s3                       's3'
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

Go code sample : Go 代码示例

package main

import (
    "fmt"
    "regexp"
)

func main() {
    r := regexp.MustCompile(`^(.*?)\.s3\b`)
    str := "bucket-name.s3.us-west-2.amazonaws.com"
    match := r.FindStringSubmatch(str)
        fmt.Println(match[1])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 aws-sdk-2.x 从 S3 存储桶中获取 object 的 S3 URL - How to get S3 URL for the object from S3 bucket using aws-sdk-2.x 有没有办法使用 aws s3 ls cli 将 S3 存储桶名称添加到存储桶的递归列表中? - Is there a way to add the S3 bucket name to the recursive list of a bucket using aws s3 ls cli? AWS S3 从存储桶中的文件夹中获取 object url - AWS S3 get object url from folder in the bucket AWS Lambda 尝试将文件从 S3 存储桶复制到另一个 S3 存储桶时出现无效存储桶名称错误 - Invalid bucket name error when AWS Lambda tries to copy files from an S3 bucket to another S3 bucket AWS S3 存储桶预签名 url 问题 - AWS S3 Bucket Presigned url issue S3:无效的存储桶名称 - 存储桶名称必须与正则表达式匹配 - S3: Invalid bucket name - Bucket name must match the regex 将文件从一个 AWS 帐户的 S3 存储桶复制到另一个 AWS 帐户的 S3 存储桶 + 使用 NodeJS - Copy files from one AWS account's S3 bucket to another AWS account's S3 bucket + using NodeJS 如何将 AWS S3 url 转换为 boto 的存储桶名称? - How do I translate an AWS S3 url into a bucket name for boto? AWS s3 试图修复错误 s3.meta.client.head_bucket(Bucket=bucket_name) - AWS s3 trying to fix error s3.meta.client.head_bucket(Bucket=bucket_name) 使用 Nodejs 的 AWS S3 存储桶到存储桶同步 - AWS S3 bucket to bucket sync using Nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM