ファイル/添付ファイルの操作
StorageカテゴリとGraphQL APIカテゴリを組み合わせて、特定のレコードに画像や動画などのファイルを関連付けることができます。例えば、プロフィール画像を持つUserモデルや、関連する画像を持つPostモデルを作成することができます。AmplifyのGraphQL APIおよびStorageカテゴリを使用すると、モデル自体の中でファイルを参照して関連付けを作成することができます。
プロジェクトのセットアップ
クイックスタートガイドの手順に従ってプロジェクトをセットアップしてください。
モデルの定義
amplify/data/resource.tsを開き、以下のモデルを追加してください:
import { type ClientSchema, a, defineData } from "@aws-amplify/backend";
const schema = a.schema({ Song: a .model({ id: a.id().required(), name: a.string().required(), coverArtPath: a.string(), }) .authorization((allow) => [allow.publicApiKey()]),});
export type Schema = ClientSchema<typeof schema>;
export const data = defineData({ schema, authorizationModes: { defaultAuthorizationMode: "apiKey",
apiKeyAuthorizationMode: { expiresInDays: 30, }, },});Storageのセットアップ
次に、Storageを設定し、アプリケーションのすべての認証済み(サインイン済み)ユーザーにアクセスを許可します。amplify/storage/resource.tsファイルを作成し、以下のコードを追加してください。これにより、ファイルアクセスがサインイン済みユーザーのみに制限されます。
import { defineStorage } from "@aws-amplify/backend";
export const storage = defineStorage({ name: "amplify-gen2-files", access: (allow) => ({ "images/*": [allow.authenticated.to(["read", "write", "delete"])], }),});以下のようにamplify/backend.tsファイルでStorageを設定してください:
import { defineBackend } from "@aws-amplify/backend";import { auth } from "./auth/resource";import { data } from "./data/resource";import { storage } from "./storage/resource";
export const backend = defineBackend({ auth, data, storage,});認可の設定
すべてのデータとファイルをパブリックにアクセス可能にする場合を除き、アプリケーションはStorageとDataの両方に対して読み取りおよび書き込みのための認可クレデンシャルが必要です。
StorageカテゴリとDataカテゴリはそれぞれ独自の認可パターンに基づいてデータアクセスを管理します。つまり、各カテゴリに対して適切な認可ロールを設定する必要があります。両カテゴリはAuthカテゴリを通じて設定された同じアクセスクレデンシャルを共有していますが、互いに独立して動作します。例えば、DataにAllow.authenticated()を追加しても、StorageカテゴリのファイルアクセスはGuardされません。同様に、Storageカテゴリに認可ルールを追加しても、APIのデータアクセスはGuardされません。
Storageを設定すると、AmplifyはCognito IDプールロールを使用してバケット上に適切なIAMポリシーを設定します。その後、認証済みユーザーとゲストユーザーがこれらのレベル内で限定的な権限を付与されるように、CRUDベース(作成、更新、読み取り、削除)の権限を追加することもできます。この設定を追加した後も、すべてのStorageアクセスはデフォルトでguestのままです。 誤ってパブリックアクセスされないよう、StorageアクセスレベルはStorageオブジェクト上でグローバルに設定するか、個々の関数呼び出しで設定する必要があります。このガイドでは前者のアプローチを使用し、すべてのStorageアクセスをauthenticatedユーザーに設定します。
各カテゴリの認可ルールを個別に設定できる機能により、データアクセスのより細かいコントロールが可能になり、柔軟性が高まります。認可パターンを混在させる必要があるシナリオでは、個々のStorage関数呼び出しにアクセスレベルを設定してください。例えば、所有者のみがアクセスすべきファイル(個人ファイルなど)にはentity_id CRUDアクセスを、ログイン済みの全ユーザーが共通ファイル(共有フォトアルバムの画像など)を閲覧できるようにするにはauthenticated読み取りアクセスを、全ユーザーがファイル(パブリックプロフィール画像など)を閲覧できるようにするにはguest読み取りアクセスを使用することができます。
Storageの認可レベルの設定方法の詳細については、Storageのドキュメントを参照してください。Dataの認可設定については、APIのドキュメントを参照してください。
関連ファイルを持つレコードの作成
Amplify Dataクライアントを使用してレコードを作成し、Storageにファイルをアップロードし、最後にレコードをアップロードしたファイルと関連付けることができます。以下の例では、Amplify Dataクライアントと、Amplify StorageライブラリのヘルパーであるuploudDataとgetUrlを使用して、レコードを作成し、ファイルをレコードに関連付けます。
import { generateClient } from "aws-amplify/api";import { uploadData, getUrl } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
// Create the API record:const response = await client.models.Song.create({ name: `My first song`,});
const song = response.data;
if (!song) return;
// Upload the Storage file:const result = await uploadData({ path: `images/${song.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional },}).result;
// Add the file association to the record:const updateResponse = await client.models.Song.update({ id: song.id, coverArtPath: result?.path,});
const updatedSong = updateResponse.data;
setCurrentSong(updatedSong);
// If the record has no associated file, we can return early.if (!updatedSong.coverArtPath) return;
// Retrieve the file's signed URL:const signedURL = await getUrl({ path: updatedSong.coverArtPath });関連レコードのファイルの追加または更新
ファイルをレコードに関連付けるには、Storageのアップロードで返されたパスでレコードを更新します。以下の例では、Storageを使用してファイルをアップロードし、ファイルのパスでレコードを更新してから、画像をダウンロードするための署名付きURLを取得します。画像がすでにレコードに関連付けられている場合、レコードは新しい画像で更新されます。
import { generateClient } from "aws-amplify/api";import { uploadData, getUrl } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
// Upload the Storage file:const result = await uploadData({ path: `images/${currentSong.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional },}).result;
// Add the file association to the record:const response = await client.models.Song.update({ id: currentSong.id, coverArtPath: result?.path,});
const updatedSong = response.data;
setCurrentSong(updatedSong);
// If the record has no associated file, we can return early.if (!updatedSong?.coverArtPath) return;
// Retrieve the file's signed URL:const signedURL = await getUrl({ path: updatedSong.coverArtPath });レコードのクエリと関連ファイルの取得
レコードに関連付けられたファイルを取得するには、まずレコードをクエリし、次にStorageを使用して署名付きURLを取得します。署名付きURLを使用してファイルをダウンロードしたり、画像を表示したりすることができます:
import { generateClient } from "aws-amplify/api";import { getUrl } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
const response = await client.models.Song.get({ id: currentSong.id,});
const song = response.data;
// If the record has no associated file, we can return early.if (!song?.coverArtPath) return;
// Retrieve the signed URL:const signedURL = await getUrl({ path: song.coverArtPath });APIレコードに関連するファイルの削除と除去
StorageファイルとGraphQL APIを操作する際によく使われる削除ワークフローは3つあります:
- ファイルの関連付けを削除し、ファイルとレコードの両方を保持し続ける。
- レコードの関連付けを削除してファイルを削除する。
- ファイルとレコードの両方を削除する。
ファイルの関連付けを削除し、ファイルとレコードの両方を保持し続ける
以下の例では、レコードからファイルの関連付けを削除しますが、S3からファイルは削除せず、データベースのレコードも削除しません。
import { generateClient } from "aws-amplify/api";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
const response = await client.models.Song.get({ id: currentSong.id,});
const song = response.data;
// If the record has no associated file, we can return early.if (!song?.coverArtPath) return;
const updatedSong = await client.models.Song.update({ id: song.id, coverArtPath: null,});レコードの関連付けを削除してファイルを削除する
以下の例では、レコードからファイルを削除し、次にS3からファイルを削除します:
import { generateClient } from "aws-amplify/api";import { remove } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});const response = await client.models.Song.get({ id: currentSong.id,});const song = response?.data;
// If the record has no associated file, we can return early.if (!song?.coverArtPath) return;
// Remove associated file from recordconst updatedSong = await client.models.Song.update({ id: song.id, coverArtPath: null,});
// Delete the file from S3:await remove({ path: song.coverArtPath });ファイルとレコードの両方を削除する
import { generateClient } from "aws-amplify/api";import { remove } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});const response = await client.models.Song.get({ id: currentSong.id,});
const song = response.data;
// If the record has no associated file, we can return early.if (!song?.coverArtPath) return;
await remove({ path: song.coverArtPath });
// Delete the record from the API:await client.models.Song.delete({ id: song.id });複数ファイルの操作
ユーザープロフィールに複数の画像を持たせるなど、1つのレコードに複数のファイルを追加したい場合があります。これを行うには、レコードにファイルキーのリストを追加できます。以下の例では、レコードにファイルキーのリストを追加します:
複数のファイルをデータモデルに関連付けるGraphQLスキーマ
amplify/data/resource.tsファイルに以下のモデルを追加してください。
const schema = a.schema({ PhotoAlbum: a .model({ id: a.id().required(), name: a.string().required(), imagePaths: a.string().array(), }) .authorization((allow) => [allow.publicApiKey()]),});複数のファイルを操作する際のCRUD操作は、単一のファイルキーではなくファイルキーのリストを操作する点を除いて、単一ファイルを操作する場合と同じです。
複数の関連ファイルを持つレコードの作成
まずGraphQL APIを使用してレコードを作成し、次にStorageにファイルをアップロードし、最後にレコードとファイルの関連付けを追加します。
import { generateClient } from "aws-amplify/api";import { uploadData, getUrl } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
// Create the API record:const response = await client.models.PhotoAlbum.create({ name: `My first photoAlbum`,});
const photoAlbum = response.data.createPhotoAlbum;
if (!photoAlbum) return;
// Upload all files to Storage:const imagePaths = await Promise.all( Array.from(e.target.files).map(async (file) => { const result = await uploadData({ path: `images/${photoAlbum.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional }, }).result;
return result.path; }));
const updatePhotoAlbumDetails = { id: photoAlbum.id, imagePaths: imagePaths,};
// Add the file association to the record:const updateResponse = await client.graphql({ query: mutations.updatePhotoAlbum, variables: { input: updatePhotoAlbumDetails },});
const updatedPhotoAlbum = updateResponse.data.updatePhotoAlbum;
// If the record has no associated file, we can return early.if (!updatedPhotoAlbum.imageKeys?.length) return;
// Retrieve signed urls for all files:const signedUrls = await Promise.all( updatedPhotoAlbum?.imagePaths.map( async (path) => await getUrl({ path: path! }) ));関連レコードへの新しいファイルの追加
レコードに追加のファイルを関連付けるには、Storageのアップロードで返されたパスでレコードを更新します。
import { generateClient } from "aws-amplify/api";import { uploadData, getUrl } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
// Upload all files to Storage:const newimagePaths = await Promise.all( Array.from(e.target.files).map(async (file) => { const result = await uploadData({ path: `images/${currentPhotoAlbum.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional }, }).result;
return result.path; }));
// Query existing record to retrieve currently associated files:const queriedResponse = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id,});
const photoAlbum = queriedResponse.data;
if (!photoAlbum?.imagePaths) return;
// Merge existing and new file paths:const updatedimagePaths = [...newimagePaths, ...photoAlbum.imagePaths];
// Update record with merged file associations:const response = await client.models.PhotoAlbum.update({ id: currentPhotoAlbum.id, imagePaths: updatedimagePaths,});
const updatedPhotoAlbum = response.data;
// If the record has no associated file, we can return early.if (!updatedPhotoAlbum?.imageKeys) return;
// Retrieve signed urls for merged image paths:const signedUrls = await Promise.all( updatedPhotoAlbum?.imagePaths.map( async (path) => await getUrl({ path: path! }) ));関連レコードのファイルの更新
関連レコードのファイルを更新する操作は、単一ファイルレコードのファイルを更新する場合と同じですが、ファイルキーのリストを更新する必要がある点が異なります。
import { generateClient } from "aws-amplify/api";import { uploadData, getUrl } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
// Upload new file to Storage:const result = await uploadData({ path: `images/${currentPhotoAlbum.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional },}).result;
const newFilePath = result.path;
// Query existing record to retrieve currently associated files:const queriedResponse = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id,});
const photoAlbum = queriedResponse.data;
if (!photoAlbum?.imagePaths?.length) return;
// Retrieve last image path:const [lastImagePath] = photoAlbum.imagePaths.slice(-1);
// Remove last file association by pathconst updatedimagePaths = [ ...photoAlbum.imagePaths.filter((path) => path !== lastImagePath), newFilePath,];
// Update record with updated file associations:const response = await client.models.PhotoAlbum.update({ id: currentPhotoAlbum.id, imagePaths: updatedimagePaths,});
const updatedPhotoAlbum = response.data;
// If the record has no associated file, we can return early.if (!updatedPhotoAlbum?.imagePaths) return;
// Retrieve signed urls for merged image paths:const signedUrls = await Promise.all( updatedPhotoAlbum?.imagePaths.map( async (path) => await getUrl({ path: path! }) ));レコードのクエリと関連ファイルの取得
レコードに関連付けられたファイルを取得するには、まずレコードをクエリし、次にStorageを使用してすべての署名付きURLを取得します。
async function getImagesForPhotoAlbum() {import { generateClient } from "aws-amplify/api";import { uploadData, getUrl } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
// Query the record to get the file paths:const response = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id,});
const photoAlbum = response.data;
// If the record has no associated files, we can return early.if (!photoAlbum?.imagePaths) return;
// Retrieve the signed URLs for the associated images:const signedUrls = await Promise.all( photoAlbum.imagePaths.map(async (imagePath) => { if (!imagePath) return; return await getUrl({ path: imagePath }); }));}APIレコードに関連するファイルの削除と除去
APIレコードに関連するファイルの削除と除去のワークフローは、単一ファイルを操作する場合と同じですが、削除を実行する際はファイルパスのリストを反復処理して各ファイルに対してStorage.remove()を呼び出す必要があります。
ファイルの関連付けを削除し、ファイルとレコードの両方を保持し続ける
import { generateClient } from "aws-amplify/api";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
const response = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id,});
const photoAlbum = response.data;
// If the record has no associated file, we can return early.if (!photoAlbum?.imagePaths) return;
const updatedPhotoAlbum = await client.models.PhotoAlbum.update({ id: photoAlbum.id, imagePaths: null,});レコードの関連付けを削除してファイルを削除する
import { generateClient } from "aws-amplify/api";import { remove } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
const response = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id,});
const photoAlbum = response.data;
// If the record has no associated files, we can return early.if (!photoAlbum?.imagePaths) return;
// Remove associated files from recordconst updateResponse = await client.models.PhotoAlbum.update({ id: photoAlbum.id, imagePaths: null, // Set the file association to `null`});
const updatedPhotoAlbum = updateResponse.data;
// Delete the files from S3:await Promise.all( photoAlbum?.imagePaths.map(async (imagePath) => { if (!imagePath) return; await remove({ path: imagePath }); }));レコードとすべての関連ファイルの削除
import { generateClient } from "aws-amplify/api";import { remove } from "aws-amplify/storage";import type { Schema } from "../amplify/data/resource";
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
const response = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id,});
const photoAlbum = response.data;
if (!photoAlbum) return;
await client.models.PhotoAlbum.delete({ id: photoAlbum.id,});
setCurrentPhotoAlbum(null);
// If the record has no associated file, we can return early.if (!photoAlbum?.imagePaths) return;
await Promise.all( photoAlbum?.imagePaths.map(async (imagePath) => { if (!imagePath) return; await remove({ path: imagePath }); }));レコードとファイルを操作する際のデータ整合性
このドキュメントで推奨するアクセスパターンは、削除されたファイルを除去しようとしますが、存在しないファイルを参照するレコードを残すよりも、孤立したファイルを残す方を優先します。これにより、クライアントが存在しないStorageファイルを取得しようとすることが_ほとんどない_ことが保証され、読み取りレイテンシが最適化されます。ただし、ファイルを削除するアプリケーションでは、_デバイス上の_レコードが存在しないファイルを参照する可能性が本質的に生じます。
一例として、APIレコードを作成し、Storageファイルをそのレコードに関連付け、ファイルの署名付きURLを取得する場合があります。「デバイスA」がGraphQL APIを呼び出してAPI_Record_1を作成し、次にそのレコードをFirst_Photoと関連付けます。「デバイスA」が署名付きURLを取得しようとする直前に、「デバイスB」がAPI_Record_1をクエリし、First_Photoを削除して、それに応じてレコードを更新する可能性があります。しかし、「デバイスA」はまだ古いAPI_Record_1を使用しており、そのレコードはすでに存在しないファイルを参照しています。共有グローバル状態がすべての段階で正しく同期されているにもかかわらず、個々のデバイス(「デバイスA」)には存在しないファイルを参照する古いレコードが残っています。同様の問題が更新時にも発生する可能性があります。アプリによっては、リアルタイムデータ / GraphQLサブスクリプションを使用することで、これらの不一致を_さらに_最小化できる場合があります。
これらの不一致がいつ発生するかを理解し、そのようなケースに対して意味のあるエラーハンドリングを追加することが重要です。このガイドには、包括的なエラーハンドリング、リアルタイムサブスクリプション、古くなったレコードの再クエリ、失敗した操作の再試行は含まれていません。ただし、これらはすべて本番レベルのアプリケーションにとって重要な考慮事項です。
完全なサンプル
import "./App.css";import { generateClient } from "aws-amplify/api";import { uploadData, getUrl, remove } from "aws-amplify/storage";import React, { useState } from "react";import type { Schema } from "../amplify/data/resource";import "@aws-amplify/ui-react/styles.css";import { type WithAuthenticatorProps, withAuthenticator,} from "@aws-amplify/ui-react";import { Amplify } from "aws-amplify";import outputs from "../amplify_outputs.json";
Amplify.configure(outputs);
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
type Song = Schema["Song"]["type"];
function App({ signOut, user }: WithAuthenticatorProps) {
const [currentSong, setCurrentSong] = useState<Song | null>(null);
// Used to display image for current song: const [currentImageUrl, setCurrentImageUrl] = useState< string | null | undefined >("");
async function createSongWithImage(e: React.ChangeEvent<HTMLInputElement>) { if (!e.target.files) return; const file = e.target.files[0]; try {
// Create the API record: const response = await client.models.Song.create({ name: `My first song`, });
const song = response.data;
if (!song) return;
// Upload the Storage file: const result = await uploadData({ path: `images/${song.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional }, }).result;
// Add the file association to the record: const updateResponse = await client.models.Song.update({ id: song.id, coverArtPath: result?.path, });
const updatedSong = updateResponse.data; setCurrentSong(updatedSong);
// If the record has no associated file, we can return early. if (!updatedSong?.coverArtPath) return;
// Retrieve the file's signed URL: const signedURL = await getUrl({ path: updatedSong.coverArtPath });
setCurrentImageUrl(signedURL.url.toString()); } catch (error) { console.error("Error create song / file:", error); } }
// Upload image, add to song, retrieve signed URL and retrieve the image. // Also updates image if one already exists. async function addNewImageToSong(e: React.ChangeEvent<HTMLInputElement>) {
if (!currentSong) return;
if (!e.target.files) return;
const file = e.target.files[0];
try { // Upload the Storage file: const result = await uploadData({ path: `images/${currentSong.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional }, }).result;
// Add the file association to the record: const response = await client.models.Song.update({ id: currentSong.id, coverArtPath: result?.path, });
const updatedSong = response.data;
setCurrentSong(updatedSong);
// If the record has no associated file, we can return early. if (!updatedSong?.coverArtPath) return;
// Retrieve the file's signed URL: const signedURL = await getUrl({ path: updatedSong.coverArtPath }); setCurrentImageUrl(signedURL.url.toString());
} catch (error) { console.error("Error uploading image / adding image to song: ", error); } }
async function getImageForCurrentSong() { if (!currentSong) return;
try { // Query the record to get the file path: const response = await client.models.Song.get({ id: currentSong.id, });
const song = response.data;
// If the record has no associated file, we can return early. if (!song?.coverArtPath) return;
// Retrieve the signed URL: const signedURL = await getUrl({ path: song.coverArtPath }); setCurrentImageUrl(signedURL.url.toString()); } catch (error) { console.error("Error getting song / image:", error); } }
// Remove the file association, continue to persist both file and record async function removeImageFromSong() {
if (!currentSong) return;
try { const response = await client.models.Song.get({ id: currentSong.id, });
const song = response.data;
// If the record has no associated file, we can return early. if (!song?.coverArtPath) return;
const updatedSong = await client.models.Song.update({ id: song.id, coverArtPath: null, });
// If successful, the response here will be `null`: setCurrentSong(updatedSong.data);
setCurrentImageUrl(updatedSong.data?.coverArtPath);
} catch (error) { console.error("Error removing image from song: ", error); } }
// Remove the record association and delete the file async function deleteImageForCurrentSong() {
if (!currentSong) return;
try { const response = await client.models.Song.get({ id: currentSong.id, });
const song = response?.data;
// If the record has no associated file, we can return early. if (!song?.coverArtPath) return;
// Remove associated file from record const updatedSong = await client.models.Song.update({ id: song.id, coverArtPath: null, });
// Delete the file from S3: await remove({ path: song.coverArtPath });
// If successful, the response here will be `null`: setCurrentSong(updatedSong.data);
setCurrentImageUrl(updatedSong.data?.coverArtPath);
} catch (error) { console.error("Error deleting image: ", error); } }
// Delete both file and record async function deleteCurrentSongAndImage() {
if (!currentSong) return; try { const response = await client.models.Song.get({ id: currentSong.id, }); const song = response.data;
// If the record has no associated file, we can return early. if (!song?.coverArtPath) return;
await remove({ path: song.coverArtPath });
// Delete the record from the API: await client.models.Song.delete({ id: song.id });
clearLocalState();
} catch (error) { console.error("Error deleting song: ", error); } }
function clearLocalState() { setCurrentSong(null); setCurrentImageUrl(""); }
return ( <> <h1>Hello {user?.username}</h1> <button onClick={signOut}>Sign out</button> <div> <label> <h2>{`Current Song: ${currentSong?.id}`}</h2> Create song with file: <input id="name" type="file" onChange={createSongWithImage} /> </label> <label> Add / update song image: <input id="name" type="file" onChange={addNewImageToSong} disabled={!currentSong} /> </label> <button onClick={getImageForCurrentSong} disabled={!currentSong || !currentImageUrl} > Get image for current song </button> <button onClick={removeImageFromSong} disabled={!currentSong || !currentImageUrl} > Remove image from current song (does not delete image) </button> <button onClick={deleteImageForCurrentSong} disabled={!currentSong || !currentImageUrl} > Remove image from current song, then delete image </button> <button onClick={deleteCurrentSongAndImage} disabled={!currentSong}> Delete current song (and image, if it exists) </button> <button onClick={signOut} className="app-button"> Sign out </button> </div> </> );}
export default withAuthenticator(App);import "./App.css";import { generateClient } from "aws-amplify/api";import { uploadData, getUrl, remove } from "aws-amplify/storage";import React, { useState } from "react";import type { Schema } from "../amplify/data/resource";import "@aws-amplify/ui-react/styles.css";import { type WithAuthenticatorProps, withAuthenticator,} from "@aws-amplify/ui-react";import { Amplify } from "aws-amplify";import outputs from "../amplify_outputs.json";
Amplify.configure(outputs);
// Generating the clientconst client = generateClient<Schema>({ authMode: "apiKey",});
type PhotoAlbum = Schema["PhotoAlbum"]["type"];
function App({ signOut, user }: WithAuthenticatorProps) { // State to hold the recognized text const [currentPhotoAlbum, setCurrentPhotoAlbum] = useState<PhotoAlbum | null>( null );
// Used to display images for current photoAlbum: const [currentImages, setCurrentImages] = useState< (string | null | undefined)[] | null | undefined >([]);
async function createPhotoAlbumWithFirstImage( e: React.ChangeEvent<HTMLInputElement> ) { if (!e.target.files) return;
const file = e.target.files[0];
try { // Create the API record: const response = await client.models.PhotoAlbum.create({ name: `My first photoAlbum`, });
const photoAlbum = response.data;
if (!photoAlbum) return;
// Upload the Storage file: const result = await uploadData({ path: `images/${photoAlbum.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional }, }).result;
const updatePhotoAlbumDetails = { id: photoAlbum.id, imagePaths: [result.path], };
// Add the file association to the record: const updateResponse = await client.models.PhotoAlbum.update({ id: photoAlbum.id, imagePaths: [result.path], });
const updatedPhotoAlbum = updateResponse.data;
setCurrentPhotoAlbum(updatedPhotoAlbum);
// If the record has no associated file, we can return early. if (!updatedPhotoAlbum?.imagePaths?.length) return;
// Retrieve the file's signed URL: const signedURL = await getUrl({ path: updatedPhotoAlbum.imagePaths[0]!, }); setCurrentImages([signedURL.url.toString()]); } catch (error) { console.error("Error create photoAlbum / file:", error); } }
async function createPhotoAlbumWithMultipleImages( e: React.ChangeEvent<HTMLInputElement> ) { if (!e.target.files) return;
try { const photoAlbumDetails = { name: `My first photoAlbum`, };
// Create the API record: const response = await client.models.PhotoAlbum.create({ name: `My first photoAlbum`, });
const photoAlbum = response.data;
if (!photoAlbum) return;
// Upload all files to Storage: const imagePaths = await Promise.all( Array.from(e.target.files).map(async (file) => { const result = await uploadData({ path: `images/${photoAlbum.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional }, }).result;
return result.path; }) );
// Add the file association to the record: const updateResponse = await client.models.PhotoAlbum.update({ id: photoAlbum.id, imagePaths: imagePaths, }); const updatedPhotoAlbum = updateResponse.data;
setCurrentPhotoAlbum(updatedPhotoAlbum);
// If the record has no associated file, we can return early. if (!updatedPhotoAlbum?.imagePaths?.length) return;
// Retrieve signed urls for all files: const signedUrls = await Promise.all( updatedPhotoAlbum.imagePaths.map( async (path) => await getUrl({ path: path! }) ) );
if (!signedUrls) return; setCurrentImages(signedUrls.map((signedUrl) => signedUrl.url.toString())); } catch (error) { console.error("Error create photoAlbum / file:", error); } }
async function addNewImagesToPhotoAlbum( e: React.ChangeEvent<HTMLInputElement> ) { if (!currentPhotoAlbum) return;
if (!e.target.files) return;
try { // Upload all files to Storage: const newimagePaths = await Promise.all( Array.from(e.target.files).map(async (file) => { const result = await uploadData({ path: `images/${currentPhotoAlbum.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional }, }).result;
return result.path; }) );
// Query existing record to retrieve currently associated files: const queriedResponse = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id, });
const photoAlbum = queriedResponse.data;
if (!photoAlbum?.imagePaths) return;
// Merge existing and new file paths: const updatedimagePaths = [...newimagePaths, ...photoAlbum.imagePaths];
// Update record with merged file associations: const response = await client.models.PhotoAlbum.update({ id: currentPhotoAlbum.id, imagePaths: updatedimagePaths, });
const updatedPhotoAlbum = response.data; setCurrentPhotoAlbum(updatedPhotoAlbum);
// If the record has no associated file, we can return early. if (!updatedPhotoAlbum?.imagePaths) return;
// Retrieve signed urls for merged image paths: const signedUrls = await Promise.all( updatedPhotoAlbum?.imagePaths.map( async (path) => await getUrl({ path: path! }) ) );
if (!signedUrls) return;
setCurrentImages(signedUrls.map((signedUrl) => signedUrl.url.toString())); } catch (error) { console.error( "Error uploading image / adding image to photoAlbum: ", error ); } }
// Replace last image associated with current photoAlbum: async function updateLastImage(e: React.ChangeEvent<HTMLInputElement>) { if (!currentPhotoAlbum) return;
if (!e.target.files) return;
const file = e.target.files[0];
try { // Upload new file to Storage: const result = await uploadData({ path: `images/${currentPhotoAlbum.id}-${file.name}`, data: file, options: { contentType: "image/png", // contentType is optional }, }).result;
const newFilePath = result.path;
// Query existing record to retrieve currently associated files: const queriedResponse = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id, });
const photoAlbum = queriedResponse.data;
if (!photoAlbum?.imagePaths?.length) return;
// Retrieve last image path: const [lastImagePath] = photoAlbum.imagePaths.slice(-1);
// Remove last file association by path const updatedimagePaths = [ ...photoAlbum.imagePaths.filter((path) => path !== lastImagePath), newFilePath, ];
// Update record with updated file associations: const response = await client.models.PhotoAlbum.update({ id: currentPhotoAlbum.id, imagePaths: updatedimagePaths, });
const updatedPhotoAlbum = response.data;
setCurrentPhotoAlbum(updatedPhotoAlbum);
// If the record has no associated file, we can return early. if (!updatedPhotoAlbum?.imagePaths) return;
// Retrieve signed urls for merged image paths: const signedUrls = await Promise.all( updatedPhotoAlbum?.imagePaths.map( async (path) => await getUrl({ path: path! }) ) );
if (!signedUrls) return;
setCurrentImages(signedUrls.map((signedUrl) => signedUrl.url.toString())); } catch (error) { console.error( "Error uploading image / adding image to photoAlbum: ", error ); } }
async function getImagesForPhotoAlbum() { if (!currentPhotoAlbum) { return; } try { // Query the record to get the file paths: const response = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id, }); const photoAlbum = response.data;
// If the record has no associated files, we can return early. if (!photoAlbum?.imagePaths) return;
// Retrieve the signed URLs for the associated images: const signedUrls = await Promise.all( photoAlbum.imagePaths.map(async (imagePath) => { if (!imagePath) return; return await getUrl({ path: imagePath }); }) );
setCurrentImages( signedUrls.map((signedUrl) => signedUrl?.url.toString()) ); } catch (error) { console.error("Error getting photoAlbum / image:", error); } }
// Remove the file associations, continue to persist both files and record async function removeImagesFromPhotoAlbum() { if (!currentPhotoAlbum) return;
try { const response = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id, });
const photoAlbum = response.data;
// If the record has no associated file, we can return early. if (!photoAlbum?.imagePaths) return;
const updatedPhotoAlbum = await client.models.PhotoAlbum.update({ id: photoAlbum.id, imagePaths: null, });
// If successful, the response here will be `null`: setCurrentPhotoAlbum(updatedPhotoAlbum.data); setCurrentImages(updatedPhotoAlbum.data?.imagePaths); } catch (error) { console.error("Error removing image from photoAlbum: ", error); } }
// Remove the record association and delete the file async function deleteImagesForCurrentPhotoAlbum() { if (!currentPhotoAlbum) return;
try { const response = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id, });
const photoAlbum = response.data;
// If the record has no associated files, we can return early. if (!photoAlbum?.imagePaths) return;
// Remove associated files from record const updateResponse = await client.models.PhotoAlbum.update({ id: photoAlbum.id, imagePaths: null, // Set the file association to `null` });
const updatedPhotoAlbum = updateResponse.data;
// Delete the files from S3: await Promise.all( photoAlbum?.imagePaths.map(async (imagePath) => { if (!imagePath) return; await remove({ path: imagePath }); }) );
// If successful, the response here will be `null`: setCurrentPhotoAlbum(updatedPhotoAlbum); setCurrentImages(null); } catch (error) { console.error("Error deleting image: ", error); } }
// Delete both files and record async function deleteCurrentPhotoAlbumAndImages() { if (!currentPhotoAlbum) return;
try { const response = await client.models.PhotoAlbum.get({ id: currentPhotoAlbum.id, });
const photoAlbum = response.data;
if (!photoAlbum) return;
await client.models.PhotoAlbum.delete({ id: photoAlbum.id, });
setCurrentPhotoAlbum(null);
// If the record has no associated file, we can return early. if (!photoAlbum?.imagePaths) return;
await Promise.all( photoAlbum?.imagePaths.map(async (imagePath) => { if (!imagePath) return; await remove({ path: imagePath }); }) );
clearLocalState(); } catch (error) { console.error("Error deleting photoAlbum: ", error); } }
function clearLocalState() { setCurrentPhotoAlbum(null); setCurrentImages([]); }
return ( <main className="app-container"> <h1 className="greeting">Hello {user?.username}!</h1> <h2 className="current-album"> Current PhotoAlbum: {currentPhotoAlbum?.id} </h2>
<div className="file-input-container"> <label className="file-input-label"> Create photoAlbum with one file: <input type="file" accept="image/*" onChange={createPhotoAlbumWithFirstImage} className="file-input" /> </label>
<label className="file-input-label"> Create photoAlbum with multiple files: <input type="file" accept="image/*" onChange={createPhotoAlbumWithMultipleImages} multiple className="file-input" /> </label>
<label className="file-input-label"> Add multiple images to current photoAlbum: <input type="file" accept="image/*" onChange={addNewImagesToPhotoAlbum} disabled={!currentPhotoAlbum} multiple className="file-input" /> </label>
<label className="file-input-label"> Replace last image: <input type="file" accept="image/*" onChange={updateLastImage} disabled={!currentPhotoAlbum || !currentImages} className="file-input" /> </label> </div>
<div className="button-container"> <button onClick={getImagesForPhotoAlbum} disabled={!currentPhotoAlbum || !currentImages} className="app-button" > Get Images for Current Photo Album </button> <button onClick={removeImagesFromPhotoAlbum} disabled={!currentPhotoAlbum || !currentImages} className="app-button" > Remove images from current PhotoAlbum (does not delete images) </button> <button onClick={deleteImagesForCurrentPhotoAlbum} disabled={!currentPhotoAlbum || !currentImages} className="app-button" > Remove images from current PhotoAlbum, then delete images </button> <button onClick={deleteCurrentPhotoAlbumAndImages} disabled={!currentPhotoAlbum} className="app-button" > Delete current PhotoAlbum (and images, if they exist) </button> <button onClick={signOut} className="app-button"> Sign out </button> </div>
<div className="image-container"> {currentImages && currentImages.map((url, idx) => { if (!url) return undefined; return ( <img src={url} key={idx} alt="Storage file" className="image" /> ); })} </div> </main> );}
export default withAuthenticator(App);