[英]Cloud Storage: how to setup service account credentials for python boto library
[英]How to setup AWS Credentials in Python
我正在尝试实现使用Amazon API的程序。 我用过yoavaviram制作的包装纸。 我将我的代码推送到github,并通知我的亚马逊我不应该在我的代码中明确地拥有我的AWS凭据。 我发现一些代码使用boto来访问AWS的桶等等,但我不认为我需要使用它。 如何在未在代码中明确写入其值的情况下,在以下代码中传递我的凭据?
#windowShopping will take all of the Amazon HTMLs from a data structure and will retrieve all of the used/new prices
import re
import json
import requests
from bs4 import BeautifulSoup
from amazon.api import AmazonAPI
import time
AMAZON_ACCESS_KEY = < my access key >
AMAZON_SECRET_KEY = < my secret key >
AMAZON_ASSOC_TAG = < my user name >
asin_regex = r'/([A-Z0-9]{10})'
isbn_regex = r'/([0-9]{10})'
def get_amazon_item_id(url):
# return either ASIN or ISBN
asin_search = re.search(asin_regex, url)
isbn_search = re.search(isbn_regex, url)
if asin_search:
return asin_search.group(1)
elif isbn_search:
return isbn_search.group(1)
else:
# log this URL
return None
def get_amazon_product_meta(url):
# the input URL is always of amazon
amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)
item_id = get_amazon_item_id(url)
if not item_id:
return None
try:
product = amazon.lookup(ItemId=item_id)
except amazon.api.AsinNotFound:
# log this ASIN
return None
except Exception:
return None
# product.price_and_currency returns in the form (price, currency)
# product_price = product.price_and_currency[0]
new_price = product._safe_get_element_text("OfferSummary.LowestNewPrice.FormattedPrice")
used_price = product._safe_get_element_text("OfferSummary.LowestUsedPrice.FormattedPrice")
trade_in_price = product._safe_get_element_text("ItemAttributes.TradeInValue.FormattedPrice")
if new_price or used_price or trade_in_price:
return new_price, used_price, trade_in_price
return Nonesting.Price.FormattedPrice
def unpickle(fileName):
f = open(fileName, 'r')
HTML_Dict = json.load(f)
print(fileName)
f.close()
return HTML_Dict
def pickle(structure,fileName):
f = open(fileName, 'w' )
json.dump(structure,f)
f.close()
def get_prices(urls,newPricesDict, usedPricesDict, tradeInDict):
#iterates through document of book urls
for url in urls:
price = get_amazon_product_meta(urls[url])
newPricesDict[url] = price[0]
usedPricesDict[url] = price[1]
tradeInDict[url] = price[2]
time.sleep(1)
print(url)
print("\t" + str(price))
def main():
newPrices = {}
usedPrices = {}
tradeInPrices = {}
urlDict = unpickle('addresses.dat')
get_prices(urlDict, newPrices, usedPrices, tradeInPrices)
pickle(newPrices, "newPrices.dat")
pickle(usedPrices, "usedPrices.dat")
pickle(tradeInPrices, "tradeInPrices.dat")
if __name__ == '__main__':
main()
创建另一个名为credentials.py
和定义变量的文件。
AMAZON_ACCESS_KEY = "access_key"
AMAZON_SECRET_KEY = "secret_key"
AMAZON_ASSOC_TAG = "tag_name"
然后在你的文件中,
from credentials import *
AMAZON_ACCESS_KEY = AMAZON_ACCESS_KEY
AMAZON_SECRET_KEY = AMAZON_SECRET_KEY
AMAZON_ASSOC_TAG = AMAZON_ASSOC_TAG
您绝对应该使用具有EC2角色的IAM凭证。 一开始有点困难,但它付出了代价。 它确保凭据不断旋转。
我不知道您正在使用的库,但是我可以告诉您,当在分配了IAM角色的EC2实例中运行时,python中的其他库会自动检测,并且它们会自动加载相应的凭据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.