1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

氷河(Glacier)に眠っている写真たちをサルベージする 〜 その2 ボールド一覧の登録

Last updated at Posted at 2024-03-25

目的

AWS S3 Glacier内のボールド一覧をMongoDBに登録する。

やり方

ListVaultsCommandを実行する。引数の指定はない。

ソースコード

バックエンド

src/app/api/listVaults/route.ts
import { NextRequest, NextResponse } from "next/server";
import {
  GlacierClient,
  ListVaultsCommand,
  DescribeVaultOutput,
} from "@aws-sdk/client-glacier";
import { MongoClient } from "mongodb";

// CORS設定
export const corsHeaders = {
  "Access-Control-Allow-Origin": "http://localhost:3000",
  "Access-Control-Allow-Methods": "POST",
  "Access-Control-Allow-Headers": "Content-Type",
};

export async function POST(request: NextRequest): Promise<NextResponse> {
  const client = new GlacierClient({});
  let values: DescribeVaultOutput[] = [];
  let input: { accountId: string; marker?: string } = {
    accountId: "",
  };
  input["accountId"] = "-";
  let command = new ListVaultsCommand(input);
  let response = await client.send(command);
  if (response.VaultList) {
    values = values.concat(response.VaultList);
  }
  while (response.Marker) {
    input["marker"] = response.Marker;
    command = new ListVaultsCommand(input);
    response = await client.send(command);
    if (response.VaultList) {
      values = values.concat(response.VaultList);
    }
  }
  const dbclient = await MongoClient.connect(
    "mongodb://127.0.0.1/27017/glacier"
  );
  const db = dbclient.db("glacier");
  const userVaults = db.collection("userVaults");
  userVaults.drop();
  for (let vault of values) {
    userVaults.insertOne(vault);
  }

  return NextResponse.json({}, { status: 200, headers: corsHeaders });
}

フロントエンド

src/app/page.tsx
"use client";
import axios from "axios";
import { Button, Stack, Paper } from "@mui/material";

export default function Home() {
  const listVaults = async () => {
    await axios.post("http://localhost:4000/api/listVaults");
  };
  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      <div className="z-10 max-w-10xl w-full items-center justify-between font-mono text-sm lg:flex">
        <Stack spacing={2}>
          <Paper>
            <Button
              variant="outlined"
              onClick={listVaults}
              style={{
                textTransform: "none",
              }}
            >
              ボールド一覧を登録する
            </Button>
          </Paper>
        </Stack>
      </div>
    </main>
  );
}

実行結果

フロントエンド

ブラウザを開き、http://localhost:3000 にアクセスする。
Screenshot from 2024-03-26 01-33-13.png
[ボールド一覧を登録する]ボタンを押すと、MongoDBにボールド一覧が登録される。

MongoDB Compass

Screenshot from 2024-03-26 01-37-17.png

その3に続く。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?