属性错误:将包含查询的 txt 文件上传到 s3 时,“int”对象没有属性“编码”

问题描述 投票:0回答:2

您好,我有一个以下形式的查询:

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,但在上传过程中我收到此错误:

[错误] AttributeError:“int”对象没有属性“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'))

    # 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
    )
python python-3.x boto3
2个回答
0
投票

所以事实证明元数据不应该是整数,所以通过这样做:

元数据 = {'_id': '123' , '名称':'新'}

解决了我的问题


0
投票

为了扩展此线程,这是此问题的回溯:

    Traceback (most recent call last):
    s3_client.put_object(\n  File \"/opt/python/botocore/client.py\", line 565, in _api_call
return self._make_api_call(operation_name, kwargs)
      File \"/opt/python/botocore/client.py\", line 958, in _make_api_call
api_params = self._emit_api_params(
  File \"/opt/python/botocore/client.py\", line 1084, in _emit_api_params
    self.meta.events.emit(
  File \"/opt/python/botocore/hooks.py\", line 412, in emit
    return self._emitter.emit(aliased_event_name, **kwargs)
      File \"/opt/python/botocore/hooks.py\", line 256, in emit
return self._emit(event_name, kwargs)
  File \"/opt/python/botocore/hooks.py\", line 239, in _emit
    response = handler(**kwargs)\n  File \"/opt/python/botocore/handlers.py\", line 631, in validate_ascii_metadata
value.encode('ascii')\nAttributeError: 'int' object has no attribute 'encode'

这里提供的解决方案也解决了我的问题

© www.soinside.com 2019 - 2024. All rights reserved.