如何测试AWS S3上是否存在存储桶 [英] How do I test if a bucket exists on AWS S3

查看:551
本文介绍了如何测试AWS S3上是否存在存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用aws-sdk测试AWS S3上是否存在存储桶?

How do I test if a bucket exists on AWS S3 using the aws-sdk?

该问题用于测试存储桶中是否存在对象:

This question is for testing if an object exists within a bucket: How to determine if object exists AWS S3 Node.JS sdk

这个问题是针对Python的:我如何检查是否存在AWS S3存储桶?

This question is for Python: How can I check that a AWS S3 bucket exists?

推荐答案

您可以使用以下代码:

// import or require aws-sdk as AWS
// const AWS = require('aws-sdk');

const checkBucketExists = async bucket => { 
  const s3 = new AWS.S3();
  const options = {
    Bucket: bucket,
  };
  try {
    await s3.headBucket(options).promise();
    return true;
  } catch (error) {
    if (error.statusCode === 404) {
      return false;
    }
    throw error;
  }
};

重要的是要认识到,如果存储桶不存在,则错误statusCode将为404.

The important thing is to realize that the error statusCode will be 404 if the bucket does not exist.

这篇关于如何测试AWS S3上是否存在存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆