亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Stubber來模擬download_fileobj?

如何使用Stubber來模擬download_fileobj?

眼眸繁星 2023-10-18 15:23:10
在boto3中,我如何使用Stubbermockdownload_fileobj這是一個資源方法?例如:import boto3from botocore.stub import Stubbers3 = boto3.resource('s3')def foo(s3):    with open('filename', 'wb') as data:        s3.download_fileobj('mybucket', 'mykey', data)def test_foo():    s3_test = boto3.resource('s3')    s3_stub = Stubber(s3_test.meta.client)        s3_stub.add_response() # something here    with s3_stub:        foo(s3_test)
查看完整描述

1 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

get_fileobj 轉換為“head_object”和“get_object”調用。這是一個對兩個調用進行存根的基本代碼片段。


bucket_name = 'mybucket'

key = 'mykey'

content = 'This is the content'


expected_params = {

    'Bucket': bucket_name,

    'Key': key,

}


# Head object

response = {

    'ContentLength': 10,

    'ContentType': 'utf-8',

    'ResponseMetadata': {

        'Bucket': bucket_name,

    }

}

s3_stub.add_response('head_object', response, expected_params)



# Get object

data = BytesIO()

data.write(content.encode("utf-8")

data.seek(0)


response = {

    'ContentLength': len(content),

    'ContentType': 'utf-8',

    'Body': data,

    'ResponseMetadata': {

        'Bucket': bucket_name,

    }

}


s3_stub.add_response('get_object', response, expected_params)


查看完整回答
反對 回復 2023-10-18
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號