-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Storage): Implementing support for multiple buckets
- Loading branch information
Showing
11 changed files
with
234 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
AmplifyPlugins/Storage/Sources/AWSS3StoragePlugin/AWSS3StoragePlugin+StorageBucket.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
@_spi(InternalAmplifyConfiguration) import Amplify | ||
import Foundation | ||
|
||
extension AWSS3StoragePlugin { | ||
/// Returns a AWSS3StorageServiceBehavior instance for the given StorageBucket | ||
func storageService(for bucket: (any StorageBucket)?) throws -> AWSS3StorageServiceBehavior { | ||
guard let bucket else { | ||
// When no bucket is provided, use the default one | ||
return storageService | ||
} | ||
|
||
let bucketInfo = try bucketInfo(from: bucket) | ||
guard let storageService = storageServices[bucketInfo.bucketName] else { | ||
let storageService = try createStorageService( | ||
authService: authService, | ||
bucketInfo: bucketInfo | ||
) | ||
storageServices[bucketInfo.bucketName] = storageService | ||
return storageService | ||
} | ||
|
||
return storageService | ||
} | ||
|
||
/// Returns a AWSS3StorageServiceProvider callback for the given StorageBucket | ||
func storageServiceProvider(for bucket: (any StorageBucket)?) -> AWSS3StorageServiceProvider { | ||
let storageServiceResolver: () throws -> AWSS3StorageServiceBehavior = { [weak self] in | ||
guard let self = self else { | ||
throw StorageError.unknown("AWSS3StoragePlugin was deallocated", nil) | ||
} | ||
return try self.storageService(for: bucket) | ||
} | ||
return storageServiceResolver | ||
} | ||
|
||
/// Returns a valid `BucketInfo` instance from the given StorageBucket | ||
private func bucketInfo(from bucket: any StorageBucket) throws -> BucketInfo { | ||
switch bucket { | ||
case let outputsBucket as OutputsStorageBucket: | ||
guard let configuredBuckets else { | ||
let errorDescription = "Amplify was not configured using an Amplify Outputs file" | ||
let recoverySuggestion = "Make sure that `Amplify.configure(with:)` is invoked" | ||
throw StorageError.validation("bucket", errorDescription, recoverySuggestion, nil) | ||
} | ||
|
||
guard let awsBucket = configuredBuckets[outputsBucket.name] else { | ||
let errorDescription = "Unable to lookup bucket from provided name in Amplify Outputs" | ||
let recoverySuggestion = "Make sure the bucket name exists in the Amplify Outputs file" | ||
throw StorageError.validation("bucket", errorDescription, recoverySuggestion, nil) | ||
} | ||
|
||
return .init( | ||
bucketName: awsBucket.bucketName, | ||
region: awsBucket.awsRegion | ||
) | ||
|
||
case let resolvedBucket as ResolvedStorageBucket: | ||
return resolvedBucket.bucketInfo | ||
|
||
default: | ||
let errorDescription = "The specified StorageBucket is not supported" | ||
let recoverySuggestion = "Please specify a StorageBucket from the Outputs file or from BucketInfo" | ||
throw StorageError.validation("bucket", errorDescription, recoverySuggestion, nil) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.