-
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 (#3817)
- Loading branch information
Showing
21 changed files
with
723 additions
and
60 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
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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
// | ||
// 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 defaultStorageService | ||
} | ||
|
||
let bucketInfo = try bucketInfo(from: bucket) | ||
guard let storageService = storageServicesByBucket[bucketInfo.bucketName] else { | ||
// If no service was found for the bucket, create one | ||
let storageService = try createStorageService( | ||
authService: authService, | ||
bucketInfo: bucketInfo | ||
) | ||
storageServicesByBucket[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 additionalBucketsByName 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 = additionalBucketsByName[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) | ||
} | ||
} | ||
} |
Oops, something went wrong.