AWS S3从友情网址获取密钥

时间:2018-04-28 03:25:35

标签: python amazon-web-services amazon-s3

有没有办法获得 键

使用 python 使用S3对象友好网址。 我知道我可以使用模式匹配,但有一种aws python方法来完成这项工作。 我熟悉java(getKey())

中可用的东西
  

https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3URI.html

例如:

https://s3.amazonaws.com/myfolder/testimage1.jpg

应输出

myfolder/testimage1.jpg

2 个答案:

答案 0 :(得分:2)

您可以使用urllib.parse,例如:

In []:
from urllib.parse import urlparse   # Py3
# from urlparse import urlparse     # Py2

url = urlparse('https://s3.amazonaws.com/myfolder/testimage1.jpg')
url.path

Out[]:
'/myfolder/testimage1.jpg'

答案 1 :(得分:0)

它只是一个URL,因此不需要任何特定的S3。最好的方法是使用function showValue(val) { console.log(val); $.getJSON('@Url.Action("GetDropdownList", "Home")' + "?value=" + val, function (result) { $("#SecondDropdown").html(""); // makes select null before filling process var data = result.data; for (var i = 0; i < data.length; i++) { $("#schoollist").append("<option value=" + data[i]["institution_id"] + ">" + data[i]["institution_name"] + "</option>") } }) } ,然后用斜杠分割:

urlparse

输出:

# For python2 use "from urlparse import urlparse" instead
from urllib.parse import urlparse
o = urlparse('https://s3.amazonaws.com/myfolder/x/y/z/testimage1.jpg')

# As this will always have a leading slash it's safe to strip
print(o.path[1:])

# Or often what you need is the s3 bucket and key separately.
bucket, key = o.path.split('/', 2)[1:]
print(bucket)
print(key)