MeWrite Docs

NoSuchBucket: The specified bucket does not exist

AWS S3で存在しないバケットにアクセスした際のエラー

概要

AWS S3で存在しないバケットにアクセスしようとした際に発生するエラーです。バケット名の間違いや、削除されたバケットへのアクセス、リージョンの不一致が原因として考えられます。

エラーメッセージ

An error occurred (NoSuchBucket) when calling the GetObject operation: The specified bucket does not exist
1
2
3
4
5
<Error>
  <Code>NoSuchBucket</Code>
  <Message>The specified bucket does not exist</Message>
  <BucketName>my-bucket-name</BucketName>
</Error>

原因

  1. バケット名の間違い: タイプミスや大文字小文字の違い
  2. バケットが存在しない: まだ作成されていない、または削除された
  3. リージョンの不一致: 異なるリージョンのバケットにアクセス
  4. 環境変数の誤り: 環境変数で指定したバケット名が間違っている
  5. アカウントの違い: 別のAWSアカウントのバケット

解決策

1. バケットの存在確認

1
2
3
4
5
6
7
8
# AWS CLIでバケット一覧を確認
aws s3 ls

# 特定のバケットの存在確認
aws s3 ls s3://my-bucket-name

# バケットの場所(リージョン)を確認
aws s3api get-bucket-location --bucket my-bucket-name

2. バケット名を確認

1
2
3
4
5
6
7
8
# バケット名は全て小文字、ハイフン・ピリオド・数字のみ
# 3〜63文字

# Good
bucket_name = "my-application-bucket-123"

# Bad
bucket_name = "My_Application_Bucket"  # 大文字・アンダースコア不可

3. リージョンを明示的に指定

1
2
3
4
5
6
7
import boto3

# リージョンを明示的に指定
s3 = boto3.client('s3', region_name='ap-northeast-1')

# または環境変数
# AWS_DEFAULT_REGION=ap-northeast-1
1
2
3
4
// AWS SDK for JavaScript
const { S3Client } = require('@aws-sdk/client-s3');

const client = new S3Client({ region: 'ap-northeast-1' });

4. バケットを作成

1
2
3
4
5
6
7
8
# バケットを作成
aws s3 mb s3://my-new-bucket --region ap-northeast-1

# または特定のリージョンで作成
aws s3api create-bucket \
  --bucket my-new-bucket \
  --region ap-northeast-1 \
  --create-bucket-configuration LocationConstraint=ap-northeast-1

5. 環境変数を確認

1
2
3
4
5
6
7
# 環境変数を確認
echo $S3_BUCKET_NAME
echo $AWS_DEFAULT_REGION

# 設定
export S3_BUCKET_NAME=my-correct-bucket
export AWS_DEFAULT_REGION=ap-northeast-1

6. エラーハンドリング

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import boto3
from botocore.exceptions import ClientError

s3 = boto3.client('s3')

try:
    response = s3.get_object(Bucket='my-bucket', Key='my-key')
except ClientError as e:
    error_code = e.response['Error']['Code']
    if error_code == 'NoSuchBucket':
        print(f"Bucket does not exist: {e}")
        # バケットを作成するか、正しいバケット名を使用
    elif error_code == 'NoSuchKey':
        print(f"Object does not exist: {e}")
    else:
        raise

7. Terraformでの対処

1
2
3
4
5
6
7
8
9
# バケットが存在することを確認
data "aws_s3_bucket" "existing" {
  bucket = "my-bucket-name"
}

# または作成
resource "aws_s3_bucket" "new" {
  bucket = "my-new-bucket-name"
}

8. CloudFormationでの対処

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-unique-bucket-name-123

  MyFunction:
    Type: AWS::Lambda::Function
    Properties:
      Environment:
        Variables:
          BUCKET_NAME: !Ref MyBucket

バケット命名規則

- 3〜63文字
- 小文字、数字、ハイフン、ピリオドのみ
- 小文字または数字で開始
- IPアドレス形式は不可(例: 192.168.0.1)
- xn--で始まることは不可
- -s3aliasで終わることは不可
- グローバルに一意

よくある間違い

  • 大文字を含むバケット名を指定する
  • 環境(dev/staging/prod)ごとに異なるバケット名を使い分け忘れる
  • バケット削除後に同じ名前で再作成できると思う(一時的に使用不可)

関連エラー

参考リンク

AWS の他のエラー

最終更新: 2025-12-13