1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
| import React, { useState } from 'react';
interface PolicyResponse {
prefix: string;
upload_url: string;
fields: {
key: string;
policy: string;
"x-goog-algorithm": string;
"x-goog-credential": string;
"x-goog-date": string;
"x-goog-signature": string;
};
}
export const FolderUpload = () => {
const [selectedFiles, setSelectedFiles] = useState<FileList | null>(null);
const [uploading, setUploading] = useState(false);
const [message, setMessage] = useState("");
const handleFileSelect = (event: React.ChangeEvent<HTMLInputElement>) => {
const files = event.target.files;
setSelectedFiles(files);
setMessage(""); // エラーメッセージを消す
};
const handleUpload = async () => {
if (!selectedFiles) {
setMessage("ファイルを選択してください");
return;
}
setUploading(true);
setMessage("");
try {
const files = Array.from(selectedFiles);
// Extract file paths only (no need to send all metadata)
const filePaths = files.map(
(file) => (file as any).webkitRelativePath || file.name,
);
// 先程作成したRails apiのエンドポイントにリクエストして署名付きポリシードキュメントを取得
const response = await fetch("/api/policy_uploads/generate_policies", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
user_id: "user123",
}),
});
if (!response.ok) {
throw new Error("ポリシードキュメントの取得に失敗しました");
}
const policy: PolicyResponse = await response.json();
// 並列アップロード
const result = await uploadWithConcurrency(files, filePaths, policy);
if (result.failed === 0) {
setMessage(`全${result.completed}ファイルのアップロードが完了しました`);
setSelectedFiles(null);
const fileInput = document.getElementById(
"folder-upload",
) as HTMLInputElement;
if (fileInput) fileInput.value = "";
} else {
setMessage(
`${result.completed}ファイル成功、${result.failed}ファイル失敗しました`,
);
}
} catch (error) {
setMessage("エラーが発生しました: " + (error as Error).message);
} finally {
setUploading(false);
}
};
const uploadWithConcurrency = async (
files: File[],
filePaths: string[],
policy: PolicyResponse,
concurrency = 8, // httpのバージョンやブラウザに合わせて調整する
) => {
let completed = 0;
let failed = 0;
const failedFiles: string[] = [];
for (let i = 0; i < files.length; i += concurrency) {
const batch = files.slice(i, i + concurrency);
const batchPaths = filePaths.slice(i, i + concurrency);
const results = await Promise.allSettled(
batch.map((file, idx) => {
return uploadFileWithPolicy(
file,
batchPaths[idx],
policy,
);
}),
);
results.forEach((result, idx) => {
const file = batch[idx];
if (result.status === "fulfilled" && result.value) {
completed++;
} else {
failed++;
failedFiles.push(file.name);
}
});
}
return { completed, failed, failedFiles };
};
const uploadFileWithPolicy = async (
file: File,
filePath: string,
policy: PolicyResponse,
maxRetries = 3, // 最大リトライ回数。必要に応じて調整してください
): Promise<boolean> => {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const formData = new FormData();
formData.append("key", policy.fields.key); // Contains "uploads/user123/${filename}"
formData.append("Content-Type", file.type || "application/octet-stream");
formData.append("policy", policy.fields.policy);
formData.append("x-goog-algorithm", policy.fields["x-goog-algorithm"]);
formData.append("x-goog-credential", policy.fields["x-goog-credential"]);
formData.append("x-goog-date", policy.fields["x-goog-date"]);
formData.append("x-goog-signature", policy.fields["x-goog-signature"]);
// 最後にファイルを設定すると決められている
// see: https://cloud.google.com/storage/docs/xml-api/post-object-forms#storage-post-policy-ruby
formData.append("file", file);
const uploaded = await new Promise<boolean>((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", (e) => {
if (e.lengthComputable) {
const percentComplete = (e.loaded / e.total) * 100;
// 割愛していますが、必要に応じてアップロードの進捗状況を取得できる
}
});
xhr.addEventListener("load", () => {
if (xhr.status === 200 || xhr.status === 204) {
resolve(true);
} else {
console.error(
`Upload failed for ${file.name} (attempt ${attempt}/${maxRetries}):`,
xhr.status,
xhr.statusText,
xhr.responseText,
);
// 400系のエラーはリトライしない
if (xhr.status >= 400 && xhr.status < 500) {
resolve(false);
} else {
reject(new Error(`HTTP ${xhr.status}: ${xhr.statusText}`));
}
}
});
xhr.addEventListener("error", () => {
reject(new Error("Network error"));
});
xhr.addEventListener("abort", () => {
reject(new Error("Upload aborted"));
});
xhr.open("POST", policy.upload_url);
xhr.send(formData);
});
if (uploaded) {
return true;
}
} catch (error) {
console.error(
`Upload error for ${file.name} (attempt ${attempt}/${maxRetries}):`,
error,
);
}
// Wait before retrying (exponential backoff)
if (attempt < maxRetries) {
await new Promise((resolve) =>
setTimeout(resolve, Math.pow(2, attempt) * 1000),
);
}
}
return false;
};
return (
<>
<h1>フォルダアップロード</h1>
<input
type="file"
id="folder-upload"
onChange={handleFileSelect}
multiple
{...{ webkitdirectory: "true", directory: "true" }}
</>
{selectedFiles && (
<div>選択されたファイル: {selectedFiles.length}個</div>
)}
<button
onClick={handleUpload}
disabled={!selectedFiles || uploading}
className="btn btn-primary upload-btn"
>
{uploading ? "アップロード中..." : "アップロード"}
</button>
{message && <div>{message}</div>}
</>
)
}
|