Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.
Gen1 DocsLegacy

Page updated May 12, 2026

ファイルのアップロード

アップロード機能の実装

注意: ストレージAPIのTransfer Accelerationを有効にする方法については、Transfer Accelerationのドキュメントを参照してください。

ファイルからのアップロード

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
}
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
RxAmplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}

Input Streamからのアップロード

private void uploadInputStream() {
try {
InputStream exampleInputStream = getContentResolver().openInputStream(uri);
Amplify.Storage.uploadInputStream(
StoragePath.fromString("public/example"),
exampleInputStream,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
} catch (FileNotFoundException error) {
Log.e("MyAmplifyApp", "Could not find file to open for input stream.", error);
}
}
private fun uploadInputStream(uri: Uri) {
val stream = contentResolver.openInputStream(uri)
Amplify.Storage.uploadInputStream(StoragePath.fromString("public/example"), stream,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadInputStream(uri: Uri) {
val stream = contentResolver.openInputStream(uri)
val upload = Amplify.Storage.uploadInputStream(StoragePath.fromString("public/example"), stream)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}.")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed")
}
}
private void uploadInputStream() {
try {
InputStream exampleInputStream = getContentResolver().openInputStream(uri);
RxProgressAwareSingleOperation<StorageUploadInputStreamResult> rxUploadOperation =
RxAmplify.Storage.uploadInputStream(StoragePath.fromString("public/example"), exampleInputStream);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
} catch (FileNotFoundException error) {
Log.e("MyAmplifyApp", "Could not find file to open for input stream.", error);
}
}

指定したバケットへのアップロード

bucketオプションを指定することで、特定のバケットへのアップロード操作を実行することもできます。Amplify Backendでのターゲットバケットの割り当て名を表す文字列を渡すことができます。

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
StorageUploadFileOptions options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
options,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val secondBucket = StorageBucket.fromOutputs("secondBucket")
val options = StorageUploadFileOptions.builder().bucket(secondBucket).build()
Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val secondBucket = StorageBucket.fromOutputs("secondBucket")
val options = StorageUploadFileOptions.builder().bucket(secondBucket).build()
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
}
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
StorageUploadFileOptions options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
RxAmplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}

あるいは、コンソールからバケット名とリージョンを指定してオブジェクトを渡すこともできます。

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
StorageUploadFileOptions options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
options,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
val options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
val options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
}
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
StorageUploadFileOptions options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
RxAmplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}

アップロードの進捗を監視する

アップロードの進捗を追跡するには、進捗リスナーコールバックを含むuploadFile APIを使用します。

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
StorageUploadFileOptions.defaultInstance(),
progress -> Log.i("MyAmplifyApp", "Fraction completed: " + progress.getFractionCompleted()),
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val options = StorageUploadFileOptions.defaultInstance()
Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options,
{ Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}") },
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val options = StorageUploadFileOptions.defaultInstance()
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options)
val progressJob = activityScope.async {
upload.progress().collect {
Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}")
}
}
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
progressJob.cancel()
}
RxProgressAwareSingleOperation<StorageUploadFileResult> upload =
RxAmplify.Storage.uploadFile("example", exampleFile);
upload
.observeProgress()
.subscribe(
progress -> Log.i("MyAmplifyApp", progress.getFractionCompleted())
);

すべてのuploadオプション

オプション説明
metadataMap<String, String>保存するオブジェクトのメタデータ。
contentTypeString保存するオブジェクトの形式を説明する標準MIMEタイプ。
bucketStorageBucketオブジェクトを保存するバケット。
serverSideEncryptionServerSideEncryptionサーバー側の暗号化アルゴリズム。
useAccelerateEndpointbooleanアクセラレーションエンドポイントを使用するかどうかのフラグ。

転送のクエリ

Amplify Androidライブラリを使用してアップロードまたはダウンロード操作がリクエストされると、まずローカルのSQLiteデータベースに永続化され、その後実行キューに追加されます。アップロードまたはダウンロードAPIから返された転送IDを使用して、ローカルデータベースにキューイングされた転送操作をクエリできます。Get-Transfer APIは、以前にエンキューされた保留中の転送を取得し、進捗変更、エラー、成功の更新を受け取るリスナーをアタッチしたり、一時停止、キャンセル、再開したりすることができます。

Amplify.Storage.getTransfer("TRANSFER_ID",
operation -> {
Log.i("MyAmplifyApp", "Current State" + operation.getTransferState());
// 更新を受け取るリスナーを設定する
operation.setOnProgress( progress -> {});
operation.setOnSuccess( result -> {});
operation.setOnError(error -> {});
// 可能なアクション
operation.pause();
operation.resume();
operation.start();
operation.cancel();
},
{
error -> Log.e("MyAmplifyApp", "Failed to query transfer", error)
}
);
Amplify.Storage.getTransfer("TRANSFER_ID",
{ operation ->
Log.i("MyAmplifyApp", "Current State" + operation.transferState)
// 更新を受け取るリスナーを設定する
operation.setOnProgress { }
operation.setOnSuccess { }
operation.setOnError { }
// 可能なアクション
operation.pause()
operation.resume()
operation.start()
operation.cancel()
},
{
Log.e("MyAmplifyApp", "Failed to query transfer", it)
}
)
try {
val operation = Amplify.Storage.getTransfer("TRANSFER_ID")
Log.i("MyAmplifyApp", "Current State" + operation.transferState)
// 更新を受け取るリスナーを設定する
operation.setOnProgress { }
operation.setOnSuccess { }
operation.setOnError { }
// 可能なアクション
operation.pause()
operation.resume()
operation.start()
operation.cancel()
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Failed to query transfer", error)
}
RxAmplify.Storage.getTransfer("TRANSFER_ID")
.subscribe(
operation -> {
Log.i("MyAmplifyApp", "Current State" + operation.getTransferState());
// 更新を受け取るリスナーを設定する
operation.setOnProgress( progress -> {});
operation.setOnSuccess( result -> {});
operation.setOnError(error -> {});
// 可能なアクション
operation.pause();
operation.resume();
operation.start();
operation.cancel();
},
error -> Log.e("MyAmplifyApp", "Failed to query transfer", error);
);

オブジェクトメタデータ付きの転送

メタデータを伴うファイルをアップロードするには、StorageUploadFileOptionsビルダーを使用します。まずhashMapオブジェクトを作成し、ビルドプロセス中にStorageUploadFileOptionsに組み込んでから、アップロード関数に渡します。

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
// メタデータを作成する
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("myKey", "myVal");
// メタデータ付きのアップロードオプションを設定する
StorageUploadFileOptions options = StorageUploadFileOptions.builder()
.metadata(userMetadata)
.build();
// アップロードを実行する
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
options,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}
fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
// メタデータを作成する
val userMetadata: MutableMap<String, String> = HashMap()
userMetadata["myKey"] = "myVal"
// メタデータ付きのアップロードオプションを設定する
val options = StorageUploadFileOptions.builder()
.metadata(userMetadata)
.build()
// アップロードを実行する
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
options,
{ result -> Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}") },
{ error -> Log.e("MyAmplifyApp", "Upload failed", error) }
)
}
fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
// メタデータを作成する
val userMetadata: MutableMap<String, String> = HashMap()
userMetadata["myKey"] = "myVal"
// メタデータ付きのアップロードオプションを設定する
val options = StorageUploadFileOptions.builder()
.metadata(userMetadata)
.build()
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options)
val progressJob = activityScope.async {
upload.progress().collect {
Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}")
}
}
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
progressJob.cancel()
}
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("myKey", "myVal");
StorageUploadFileOptions options = StorageUploadFileOptions.builder()
.metadata(userMetadata)
.build();
RxStorageBinding.RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
RxAmplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}

署名付きURLを使用したアップロード

StorageAccessMethod.PUTを指定したgetUrl APIを使用して、S3に直接ファイルをアップロードするための署名付きURLを生成できます。これは以下のような場合に便利です:

  • 標準HTTPURLエンドポイントのみを受け付けるサードパーティのツールやライブラリと統合する必要がある場合
  • 一時的なアップロードリンクを別のクライアントやサービスと共有したい場合
  • Amplify SDKが利用できないコンテキストからアップロードする必要がある場合
AWSS3StorageGetPresignedUrlOptions options = AWSS3StorageGetPresignedUrlOptions.builder()
.method(StorageAccessMethod.PUT)
.expires(3600)
.build();
Amplify.Storage.getUrl(
StoragePath.fromString("public/uploads/photo.jpg"),
options,
result -> {
URL presignedUrl = result.getUrl();
Log.i("MyAmplifyApp", "Presigned upload URL: " + presignedUrl);
},
error -> Log.e("MyAmplifyApp", "Failed to generate URL", error)
);
val options = AWSS3StorageGetPresignedUrlOptions.builder()
.method(StorageAccessMethod.PUT)
.expires(3600)
.build()
Amplify.Storage.getUrl(
StoragePath.fromString("public/uploads/photo.jpg"),
options,
{ Log.i("MyAmplifyApp", "Presigned upload URL: ${it.url}") },
{ Log.e("MyAmplifyApp", "Failed to generate URL", it) }
)
val options = AWSS3StorageGetPresignedUrlOptions.builder()
.method(StorageAccessMethod.PUT)
.expires(3600)
.build()
try {
val result = Amplify.Storage.getUrl(
StoragePath.fromString("public/uploads/photo.jpg"),
options
)
Log.i("MyAmplifyApp", "Presigned upload URL: ${result.url}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Failed to generate URL", error)
}
AWSS3StorageGetPresignedUrlOptions options = AWSS3StorageGetPresignedUrlOptions.builder()
.method(StorageAccessMethod.PUT)
.expires(3600)
.build();
RxAmplify.Storage.getUrl(StoragePath.fromString("public/uploads/photo.jpg"), options)
.subscribe(
result -> Log.i("MyAmplifyApp", "Presigned upload URL: " + result.getUrl()),
error -> Log.e("MyAmplifyApp", "Failed to generate URL", error)
);

次に署名付きURLを使用して標準HTTPのPUTリクエストでファイルをアップロードします:

val presignedUrl = result.url
val connection = presignedUrl.openConnection() as HttpURLConnection
connection.doOutput = true
connection.requestMethod = "PUT"
connection.setRequestProperty("Content-Type", "image/jpeg")
connection.outputStream.use { outputStream ->
outputStream.write(imageData)
}
val responseCode = connection.responseCode
Log.i("MyAmplifyApp", "Upload status: $responseCode")
connection.disconnect()

StorageAccessMethod.PUTが指定された場合、オブジェクトがまだ存在しない可能性があるため、validateObjectExistenceオプションは無視されます。

署名付きURLアップロードオプション

オプションデフォルト説明
methodStorageAccessMethodGETGETはダウンロードURLを生成します。PUTはアップロードURLを生成します。
expiresint18000URLが期限切れになるまでの秒数。
bucketStorageBucketAmplify設定のデフォルトバケットオブジェクトが保存されているバケット。
validateObjectExistencebooleanfalseURLを生成する前にオブジェクトが存在するかチェックするかどうか。methodがPUTの場合はスキップされます。
useAccelerateEndpointbooleanfalseS3 Transfer Accelerationエンドポイントを使用するかどうか。

マルチパートアップロード

Amplifyは5MBを超えるオブジェクトに対して自動的にAmazon S3マルチパートアップロードを実行します。S3のマルチパートアップロードの詳細については、マルチパートアップロードを使用したオブジェクトのアップロードとコピーを参照してください。