【发布时间】:2021-10-03 02:33:39
【问题描述】:
我刚刚完成了以下将客户数据从我的 shopify 获取到 S3 存储桶的功能。现在发生的事情如下。触发器每天运行此 lambda。然后,将所有客户写入 S3 存储桶。每个已经存在的条目都会被覆盖。添加了新客户。
我的问题是:这是一种可扩展的方法,还是我应该读取所有文件并比较时间戳以仅添加新条目?还是第二种方法可能更糟?
import requests
import json
import boto3
s3 = boto3.client('s3')
bucket ='testbucket'
url2 = "something.json"
def getCustomers():
r = requests.get(url2)
return r.json()
def lambda_handler(event, context):
data = getCustomers()
for customer in data["customers"]:
#create a unique id for each customer
customer_id = str(customer["id"])
#create a file name to put the customer in bucket
file_name = 'customers' + '/' + customer_id + '.json'
#Saving .json to s3
customer_string = str(customer)
uploadByteStream = bytes(customer_string.encode('UTF-8'))
s3.put_object(Bucket=bucket, Key=file_name, Body=uploadByteStream)
return {
'statusCode': 200,
'body': json.dumps('Success')
}
示例响应如下:
{
"id": 71806090000,
"email": "something@gmail.com",
"accepts_marketing": false,
"created_at": "2021-07-27T11:06:38+02:00",
"updated_at": "2021-07-27T11:11:58+02:00",
"first_name": "Bertje",
"last_name": "Bertens",
"orders_count": 0,
"state": "disabled",
"total_spent": "0.00",
"last_order_id": null,
"note": "",
"verified_email": true,
"multipass_identifier": null,
"tax_exempt": false,
"phone": "+32470000000",
"tags": "",
"last_order_name": null,
"currency": "EUR",
"addresses": [
{
"id": 6623179276486,
"customer_id": 5371846099142,
"first_name": "Bertje",
"last_name": "Bertens",
"company": "",
"address1": "Somewhere",
"address2": "",
"city": "Somecity",
"province": null,
"country": "",
"zip": "0000",
"phone": null,
"name": "Bertje Bertens",
"province_code": null,
"country_code": null,
"country_name": "",
"default": true
}
],
"accepts_marketing_updated_at": "2021-07-27T11:11:35+02:00",
"marketing_opt_in_level": null,
"tax_exemptions": [],
"admin_graphql_api_id": "",
"default_address": {
"id": 6623179276486,
"customer_id": 5371846099142,
"first_name": "Bertje",
"last_name": "Bertens",
"company": "",
"address1": "Somewhere",
"address2": "",
"city": "Somecity",
"province": null,
"country": "",
"zip": "0000",
"phone": null,
"name": "Bertje Bertens",
"province_code": null,
"country_code": null,
"country_name": "",
"default": true
}
}
【问题讨论】:
-
您从 API 获得了什么数据?每个用户是否有类似
last_modified时间戳的东西?您可以添加示例响应吗? -
是的,有一个更新的时间和创建时间的时间戳。我还添加了一个完整的示例。
标签: amazon-web-services amazon-s3 aws-lambda processing-efficiency