簡體   English   中英

如何在Python中設置AWS憑據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM