AWS S3 NoSuchBucket异常在救援条款中未被捕获

时间:2022-11-18 23:04:04

I'm trying to get a bucket in Ruby using the AWS SDK, and trying to catch a NoSuchBucket error. Problem is, my rescue block is not catching the error, and so my app crashes. Here is the relevant code:

我正在尝试使用AWS SDK在Ruby中获取一个存储桶,并尝试捕获NoSuchBucket错误。问题是,我的救援块没有捕获错误,所以我的应用程序崩溃了。这是相关的代码:

begin
    b = s3.buckets[bucket_name]

rescue AWS::S3::Errors::NoSuchBucket
    puts Invalid bucket name.
    exit 1
end

and the error message is:

并且错误消息是:

C:/Ruby193/lib/ruby/gems/1.9.1/gems/aws-sdk-1.5.6/lib/aws/core/client.rb:277:in
`return_or_raise': The specified bucket does not exist (AWS::S3::Errors::NoSuchBucket)

Am I just making a stupid beginner syntax error, or is there a bug in the AWS code that's not actually throwing the error? I've also tried catching all errors and still no dice.

我只是犯了一个愚蠢的初学者语法错误,或者AWS代码中是否存在实际上没有引发错误的错误?我也试过捕捉所有的错误,但仍然没有骰子。

1 个解决方案

#1


3  

b = s3.buckets[bucket_name]

Doesn't actually make any requests and won't ever through exceptions like NoSuchBucket.

实际上不会发出任何请求,也不会通过像NoSuchBucket这样的异常。

It just returns a bucket object that knows what its name is. A request only happens when you actually try to do something with the bucket (list its contents, add a file to it) and it is at this point that NoSuchBucket is raised. This is outside of your begin block and so your rescue doesn't handle it. If you need to rescue that exception, you need to be putting your begin/rescue around the places that you actually use the bucket.

它只返回一个知道其名称的桶对象。只有在您实际尝试对存储桶执行某些操作(列出其内容,向其添加文件)时才会发生请求,此时此时会引发NoSuchBucket。这是在你的开始区之外,所以你的救援不会处理它。如果您需要拯救该异常,则需要将您的开始/救援放在实际使用该存储桶的地方。

If you are just trying to validate that it actually exists you could do something like

如果你只是想验证它确实存在,你可以做类似的事情

s3.buckets[bucket_name].exists?

#1


3  

b = s3.buckets[bucket_name]

Doesn't actually make any requests and won't ever through exceptions like NoSuchBucket.

实际上不会发出任何请求,也不会通过像NoSuchBucket这样的异常。

It just returns a bucket object that knows what its name is. A request only happens when you actually try to do something with the bucket (list its contents, add a file to it) and it is at this point that NoSuchBucket is raised. This is outside of your begin block and so your rescue doesn't handle it. If you need to rescue that exception, you need to be putting your begin/rescue around the places that you actually use the bucket.

它只返回一个知道其名称的桶对象。只有在您实际尝试对存储桶执行某些操作(列出其内容,向其添加文件)时才会发生请求,此时此时会引发NoSuchBucket。这是在你的开始区之外,所以你的救援不会处理它。如果您需要拯救该异常,则需要将您的开始/救援放在实际使用该存储桶的地方。

If you are just trying to validate that it actually exists you could do something like

如果你只是想验证它确实存在,你可以做类似的事情

s3.buckets[bucket_name].exists?