首页 > 解决方案 > 如何调用在 __init__ 方法中初始化的类对象?

问题描述

我有一个 s3class,它有一个__s3配置了 s3 访问密钥对的属性。

class s3class():
    def __init__(self, access, secret, bucket_name):
        self.__bucket = bucket_name
        self.__s3 = boto3.client('s3',
                                 self.__access,
                                 self.__secret)

import s3class

## do something here##
obj = s3class()
obj.__s3.download_file('prefix/filename.csv')

我想download_file在另一个导入 s3class 的模块中使用 __s3 对象调用 boto3 特定方法。当我将 __s3 对象引用为 ob.__s3 时,我得到 AttributeError 对象 __s3 未找到。

标签: amazon-web-servicesamazon-s3boto3

解决方案


如果你的模块包含s3class类被调用s3class.py,那么你需要:

from s3class import s3class

推荐阅读