Python s3上处理缺失文件的有效方法

Python s3上处理缺失文件的有效方法,python,Python,clients.py """ Wraps boto3 to provide client and resource objects that connect to Localstack when running tests if LOCALSTACK_URL env variable is set, otherwise connect to real AWS. """ import os import boto3 # Port 45

clients.py

"""
Wraps boto3 to provide client and resource objects that connect to Localstack when
running tests if LOCALSTACK_URL env variable is set, otherwise connect to real AWS.
"""
import os

import boto3

# Port 4566 is the universal "edge" port for all services since localstack v0.11.0,
# so LOCALSTACK_URL can be of the form "http://hostname:4566".
# "None" means boto3 will use the default endpoint for the real AWS services.
ENDPOINT_URL = os.getenv("LOCALSTACK_URL", None)


def s3_resource():
    return boto3.resource("s3", endpoint_url=ENDPOINT_URL)


def s3_client():
    return boto3.client("s3", endpoint_url=ENDPOINT_URL)
from mymod.aws.clients import s3_resource
   s3 = s3_resource()


   def __get_object_etag(self, s3_dir_to_download_file_from: str, file_name: str) -> str
            bucket, key = s3.deconstruct_s3_path(
                f"{s3_dir_to_download_file_from}/{file_name}"
            )
            try:
                etag_value = s3_resource().Object(bucket, key).e_tag
                return etag_value
            except botocore.exceptions.ClientError:
                raise
mycode.py

"""
Wraps boto3 to provide client and resource objects that connect to Localstack when
running tests if LOCALSTACK_URL env variable is set, otherwise connect to real AWS.
"""
import os

import boto3

# Port 4566 is the universal "edge" port for all services since localstack v0.11.0,
# so LOCALSTACK_URL can be of the form "http://hostname:4566".
# "None" means boto3 will use the default endpoint for the real AWS services.
ENDPOINT_URL = os.getenv("LOCALSTACK_URL", None)


def s3_resource():
    return boto3.resource("s3", endpoint_url=ENDPOINT_URL)


def s3_client():
    return boto3.client("s3", endpoint_url=ENDPOINT_URL)
from mymod.aws.clients import s3_resource
   s3 = s3_resource()


   def __get_object_etag(self, s3_dir_to_download_file_from: str, file_name: str) -> str
            bucket, key = s3.deconstruct_s3_path(
                f"{s3_dir_to_download_file_from}/{file_name}"
            )
            try:
                etag_value = s3_resource().Object(bucket, key).e_tag
                return etag_value
            except botocore.exceptions.ClientError:
                raise
我想知道在mycode.py中,当我要查找的文件在s3上不存在时,我是否在进行正确的错误处理。我基本上希望密钥在那个里,若并没有,我想提出错误,我不想继续,因为这段代码将作为依赖于前面每个步骤的管道的一部分使用

我很想知道我是正确处理错误,还是错误处理,那么如何处理这个案件呢


根据我对“例外”的理解假设您处理错误并继续进行,但在我的情况下,如果在S3上查找的文件丢失,我不想在代码的其余部分继续进行。

我认为此讨论可能会有所帮助:对,因此我的问题是,如果我将其作为例外处理,我的其余代码是否会继续退出@Gevorgdavoian据我所知,你不想处理那个异常,你只是在提出它。IMHO,您可以完全省略try-except块(您假设密钥确实存在,因此无法继续)。是的,任何未处理的异常都会传播并导致程序中止执行。我认为这一讨论可能会有所帮助:对,所以我的问题是,如果我将其作为异常处理,我的其余代码会继续退出吗@Gevorgdavoian据我所知,你不想处理那个异常,你只是在提出它。IMHO,您可以完全省略try-except块(您假设密钥确实存在,因此无法继续)。是的,任何未处理的异常都会传播并导致程序中止执行。