create a new folder everyday as per UTC time in my s3 bucket and save json files in it

huangapple go评论70阅读模式
英文:

create a new folder everyday as per UTC time in my s3 bucket and save json files in it

问题

错误提示表明's3.ServiceResource'对象没有'head_bucket'或'head_object'属性。这是因为在Boto3版本中,没有这些方法。要解决这个问题,你可以使用boto3.client对象来执行这些操作,而不是使用s3.resource对象。

以下是修正后的代码:

import pytz
import datetime
import boto3

# 创建S3客户端
s3 = boto3.client('s3')

# S3桶的名称
bucket_name = 'my_data'

# 获取当前UTC时间
utc_now = datetime.datetime.now(pytz.utc)
today = utc_now.strftime("%Y-%m-%d")
folder_name = "json_files_" + today

# 检查今天的文件夹是否已经存在
try:
    s3.head_object(Bucket=bucket_name, Key=folder_name + "/")
except s3.exceptions.ClientError as e:
    # 如果文件夹不存在,创建它
    if e.response["Error"]["Code"] == "404":
        s3.put_object(Bucket=bucket_name, Key=folder_name + "/")
    else:
        # 抛出其他错误
        raise

# 你的字典数据
my_dict = {"id": "0"}

# 创建JSON文件的键
key = folder_name + "/" + str(my_dict["id"]) + ".json"
print('key -> ', key)

# 上传字典数据到S3
s3.put_object(Bucket=bucket_name, Key=key, Body=json.dumps(my_dict))

这个代码使用boto3.client来执行S3操作,修复了你的问题。如果还有其他问题,你可以在这里提出,我会帮助你解决。

英文:

I want to create a new folder everyday as per UTC time in my s3 bucket, the dict should be saved in json format in that folder

My_Attempt

            import pytz
            import datetime
            import botocore
            import boto3
            
            s3 = session.resource('s3')
            config['S3_BUCKET'] = 'my_data'
            # Get the current date and time in UTC
            utc_now = datetime.datetime.now(pytz.utc)
            today = utc_now.strftime("%Y-%m-%d")
            folder_name = "json_files_" + today
            my_array = '{"id": "0"}'
            # Check if the folder for today already exists
            try:
                s3.head_bucket(Bucket=config['S3_BUCKET'], Key=folder_name + "/") #head_object(Bucket=config['S3_BUCKET'], Key=folder_name + "/")
            except botocore.exceptions.ClientError as e: #boto3.exceptions.ClientError as e: #s3.exceptions.ClientError as e:
                # If the folder does not exist, create it
                if e.response["Error"]["Code"] == "404":
                    s3.put_object(Bucket=config['S3_BUCKET'], Key=folder_name + "/")
                else:
                    # Raise any other errors
                    raise
            key = folder_name + "/" + str(my_array['id']) + ".json"
            print('key -> ',key)
            s3.put_object(Bucket="my-bucket", Key=key, Body=my_array)

ERROR
I tried both 'head_bucket' and 'head_object'

's3.ServiceResource' object has no attribute 'head_bucket' 
's3.ServiceResource' object has no attribute 'head_object'

How can I improve the code and debug the code?

答案1

得分: 0

我将s3 = session.resource('s3')替换为s3 = session.client('s3'),如@Anon Conward建议的那样,它有效。

英文:

I replaced s3 = session.resource('s3') with s3 = session.client('s3') and it works as suggested by @Anon Conward

huangapple
  • 本文由 发表于 2023年2月10日 06:27:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定