AttributeError: ‘int’对象没有属性’encode,当上传包含查询的txt文件到S3时

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

AttributeError: 'int' object has no attribute 'encode when uploading txt file containing query to s3

问题

我有一个查询关于这个表单:

data = {
    "pg_content": "CREATE OR REPLACE TABLE CREDITCARDS.CC_TRANSACTION(\n    TRANSACTION_ID    DECIMAL   COMMENT 'Identifier.Values between 0 and 23162883'\n    ACCOUNT_NUMBER    VARCHAR   COMMENT 'Categorical Attribute with specific values as ACC2060,ACC1188,ACC1437,ACC5552,ACC98,ACC2240,ACC4096,ACC5555,ACC22,ACC4232'\n    TRANSACTION_AMOUNT    DECIMAL   COMMENT 'Values between -6125.96 and 600.00'\n    TRANSACTION_DATE    DATE   COMMENT 'Values between 2023-01-09 and 2023-06-16'\n    TRANSACTION_TYPE    VARCHAR   COMMENT 'Categorical Attribute with specific values as Purchase,Cash Advance,Void,Refund,Verification,Payment'\n    MERCHANT_NAME    VARCHAR   COMMENT 'Categorical Attribute with specific values as Merchant 250575,Merchant 265897,Merchant 54632,Merchant 100866,Merchant 749929,Merchant 250268,Merchant 486642,Merchant 27292,Merchant 250396,Merchant 108175',\n    PRIMARY KEY(TRANSACTION_ID),\n    FOREIGN KEY(ACCOUNT_NUMBER) REFERENCES CREDITCARDS.CREDIT_CARD_ACCOUNT(ACCOUNT_NUMBER)\n); "
}

我想将这个内容写入一个txt文件并上传到S3,但在上传时出现了这个错误:

[ERROR] AttributeError: 'int' object has no attribute 'encode

我的代码是:

local_file_name = 'local_file'
metadata = {'_id': 123, 'name': 'new'}
text_file_path = '/tmp/' + str(local_file_name) + '.csv'

s3_client = boto3.client('s3')
with open(text_file_path, 'w') as file:
    file.write(data.get('pg_content'))

# 上传文本文件到S3
s3_client.upload_file(text_file_path, bucket_name, file_name)

s3_client.put_object(
    Bucket=bucket_name,
    Key=file_name,
    Metadata=metadata
)
英文:

Hi I have a query in this form :

data = {
    "pg_content": "CREATE OR REPLACE TABLE CREDITCARDS.CC_TRANSACTION(\n    TRANSACTION_ID    DECIMAL   COMMENT 'Identifier.Values between 0 and 23162883'\n    ACCOUNT_NUMBER    VARCHAR   COMMENT 'Categorical Attribute with specific values as ACC2060,ACC1188,ACC1437,ACC5552,ACC98,ACC2240,ACC4096,ACC5555,ACC22,ACC4232'\n    TRANSACTION_AMOUNT    DECIMAL   COMMENT 'Values between -6125.96 and 600.00'\n    TRANSACTION_DATE    DATE   COMMENT 'Values between 2023-01-09 and 2023-06-16'\n    TRANSACTION_TYPE    VARCHAR   COMMENT 'Categorical Attribute with specific values as Purchase,Cash Advance,Void,Refund,Verification,Payment'\n    MERCHANT_NAME    VARCHAR   COMMENT 'Categorical Attribute with specific values as Merchant 250575,Merchant 265897,Merchant 54632,Merchant 100866,Merchant 749929,Merchant 250268,Merchant 486642,Merchant 27292,Merchant 250396,Merchant 108175',\n    PRIMARY KEY(TRANSACTION_ID),\n    FOREIGN KEY(ACCOUNT_NUMBER) REFERENCES CREDITCARDS.CREDIT_CARD_ACCOUNT(ACCOUNT_NUMBER)\n); "
}

I want to write this content to a txt file and upload it to s3 but during upload I am getting this error :

[ERROR] AttributeError: 'int' object has no attribute 'encode

My code is :

local_file_name = 'local_file'
 metadata = {'_id':123 , 'name':'new'}
 text_file_path = '/tmp/' + str(local_file_name)  + '.csv'

    s3_client =  boto3.client('s3')
    with open(text_file_path, 'w') as file:
        file.write(data.get('pg_content'))

    # Upload the text file to S3
    s3_client.upload_file(text_file_path, bucket_name, file_name)

    s3_client.put_object(
        Bucket=bucket_name,
        Key=file_name,
        Metadata=metadata
    )

答案1

得分: 0

所以结果是元数据不应该是整数,所以通过这样做:

metadata = {'_id': '123', 'name': 'new'}

解决了我的问题。

英文:

So This turn out to be that Metadata should not be integer so by doing this:

metadata = {'_id': '123' , 'name':'new'}

solved my problem

huangapple
  • 本文由 发表于 2023年7月14日 02:01:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682117.html
匿名

发表评论

匿名网友

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

确定