I'm working with AWS Amplify to develop an iOS application. I've added storage through S3 to host some assets and am trying to configure the application to download them. The only issue is that every example I see has the bucket name and path hardcoded, but because I have multiple environments and make new environments sometimes and each bucket has the environment name appended to it, I don't want to have to rewrite the bucket name each time.
For example if I'm in my test environment the bucket name might be assetsxxxxxx-test but if I switch to a new environment, I might be referencing assetsyyyyy-dev let's say.
The thing is the bucket name is referenced in the aswconfiguration.json file:
"S3TransferUtility": {
"Default": {
"Bucket": "assetsxxxxx-test",
"Region": "us-east-2"
}
}
So my question is how can I reference that bucket name programmatically so that when that field is rewritten when I switch environments, I won't have to change my code.
Thanks
I solved it. For anybody else wondering, I'm still not sure if there's a field built in to the AWS SDK, but I decided to parse the awsconfiguration.json file directly int a custom struct:
struct AWSConfigurationJSON: Codable{
let S3TransferUtility: S3TransferUtility
}
struct S3TransferUtility: Codable{
let Default: S3TransferUtilityDefault
}
struct S3TransferUtilityDefault: Codable{
let Bucket: String
let Region: String
}
and then I read the file and parsed the JSON.
if let path = Bundle.main.path(forResource: "awsconfiguration", ofType: "json") {
do{
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let jsonResult = try JSONDecoder().decode(AWSConfigurationJSON.self, from: data)
print(jsonResult.S3TransferUtility.Default.Bucket)
bucketPath = jsonResult.S3TransferUtility.Default.Bucket
}catch let e{
print("error \(e)")
bucketPath = ""
}
}else{
bucketPath = ""
}