Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 打印消息,而不是错误S3策略BOTO3_Amazon Web Services_Amazon S3_Boto3 - Fatal编程技术网

Amazon web services 打印消息,而不是错误S3策略BOTO3

Amazon web services 打印消息,而不是错误S3策略BOTO3,amazon-web-services,amazon-s3,boto3,Amazon Web Services,Amazon S3,Boto3,我应该为存在的bucket获取策略,对于不存在olicy的bucket,只需要一个正常的错误语句。使用以下代码获取s3 bucket策略: s3=boto3.client("s3",aws_access_key_id=access_key_id,aws_secret_access_key=secret_key) for i in Bucket_Name: #Bucket_name stores the name of

我应该为存在的bucket获取策略,对于不存在olicy的bucket,只需要一个正常的错误语句。使用以下代码获取s3 bucket策略:

s3=boto3.client("s3",aws_access_key_id=access_key_id,aws_secret_access_key=secret_key)
for i in Bucket_Name:                              #Bucket_name stores the name of the buckets
     policy = s3.get_bucket_policy(Bucket=i)
     print(policy['Policy'])
现在,对于有策略的bucket,它会打印hem,但对于没有策略的bucket,它会给出以下错误,并停止进一步的执行。 botocore.exceptions.ClientError:调用GetBucketPolicy操作时,NoSuchBucketPolicy发生错误:bucket策略不存在

是否有方法打印一些消息而不是此错误,并继续执行所有其他bucket的代码?

Botocore异常是在Botocore包中静态定义的。您创建的任何Boto3客户端都将使用这些静态定义的异常类。您将遇到的最常见的botocore异常是ClientError。这也是代码中的同一错误。当AWS服务向您的Boto3客户端请求提供错误响应时,这是一个一般异常

像这样试试

import botocore
import boto3

client = boto3.client('aws_service_name')

try:
    client.some_api_call(SomeParam='some_param')
    #your code here
    for i in Bucket_Name:
      policy = s3.get_bucket_policy(Bucket=i)
      print(policy['Policy'])

except botocore.exceptions.ClientError as error:
    # Put your error handling logic here
    

except botocore.exceptions.ParamValidationError as error:
    raise ValueError('The parameters you provided are incorrect: {}'.format(error)) #for parameter validation kind of error
Botocore异常在Botocore包中静态定义。您创建的任何Boto3客户端都将使用这些静态定义的异常类。您将遇到的最常见的botocore异常是ClientError。这也是代码中的同一错误。当AWS服务向您的Boto3客户端请求提供错误响应时,这是一个一般异常

像这样试试

import botocore
import boto3

client = boto3.client('aws_service_name')

try:
    client.some_api_call(SomeParam='some_param')
    #your code here
    for i in Bucket_Name:
      policy = s3.get_bucket_policy(Bucket=i)
      print(policy['Policy'])

except botocore.exceptions.ClientError as error:
    # Put your error handling logic here
    

except botocore.exceptions.ParamValidationError as error:
    raise ValueError('The parameters you provided are incorrect: {}'.format(error)) #for parameter validation kind of error

此代码将处理以下例外情况:

s3 = boto3.client("s3", aws_access_key_id=access_key_id, aws_secret_access_key=secret_key)
for i in Bucket_Name:
    try:  # Bucket_name stores the name of the buckets
        policy = s3.get_bucket_policy(Bucket=i)
        print(policy['Policy'])
    except ClientError:
        print(f'The bucket {i} does not have a policy')

此代码将处理以下例外情况:

s3 = boto3.client("s3", aws_access_key_id=access_key_id, aws_secret_access_key=secret_key)
for i in Bucket_Name:
    try:  # Bucket_name stores the name of the buckets
        policy = s3.get_bucket_policy(Bucket=i)
        print(policy['Policy'])
    except ClientError:
        print(f'The bucket {i} does not have a policy')

添加了一个解决方案,以及来自官方boto页面的文档:添加了一个解决方案,以及来自官方boto页面的文档:引发以下错误:NameError:名称'ClientError'未从botocore定义。异常导入ClientErroryes,获得了它。谢谢引发以下错误:NameError:name'ClientError'不是从botocore定义的。异常导入ClientErroryes,获取它。谢谢