"=============="
分成上下兩部份來看:
xxx@gmail.com/生魚片/20230930161239.png
可以分別拿到使用者名稱、魚魚名稱、打成時戳樣子的檔案名稱。func fetchStamp() async {
print("Fetch Amplify S3 Fish dir")
let attrs = await fetchUserAttr()
let username = attrs["name"] ?? "guest"
var toBeDownloadedList:[String] = []
// Base Dir
let manager = FileManager.default
guard let url = manager.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}
do {
let options = StorageListRequest.Options(path:username, pageSize: 1000)
let listResult = try await Amplify.Storage.list(options: options)
listResult.items.forEach { item in
print("Key: \(item.key)")
toBeDownloadedList.append(item.key)
// Key: email/生魚片/20230930161239.png
}
} catch {
print("failed to fetch s3 file key")
}
print("==============")
for picKey in toBeDownloadedList {
print(picKey)
let pathSplit = picKey.split(separator: ["/"])
let localFileUrl = url.appendingPathComponent("saved/pics/\(pathSplit[1])/\(pathSplit[2])")
// Create fish dir
let localFishDir = url.appendingPathComponent("saved/pics/\(pathSplit[1])")
do {
try manager.createDirectory(at: localFishDir, withIntermediateDirectories: true)
} catch {
print(error)
}
// Start download
let downloadTask = Amplify.Storage.downloadFile(
key: picKey,
local: localFileUrl,
options: nil
)
do {
try await downloadTask.value
print("Completed: \(picKey)")
stamps.append(Stamp(imgName: "/saved/pics/\(pathSplit[1])/\(pathSplit[2])",
fishName: String(pathSplit[1]),
catched: 1, counted: 1)
)
} catch {
print("failed to download")
}
}
}