Skip to content

Instantly share code, notes, and snippets.

@wesww
Last active July 12, 2021 07:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wesww/8760eff3aaa74b3a3bbbf6359673baf6 to your computer and use it in GitHub Desktop.
Save wesww/8760eff3aaa74b3a3bbbf6359673baf6 to your computer and use it in GitHub Desktop.
AWS JS libs without an Amplify backend
import Auth from '@aws-amplify/auth'
import Analytics from '@aws-amplify/analytics'
Auth.configure({
region: 'us-east-1',
identityPoolId: 'us-east-1:xxxx...',
})
Analytics.configure({
AWSPinpoint: {
appId: 'appIdKey',
region: 'us-east-1'
}
})
Analytics.record({ name: 'Hello' })
Auth.configure({
region: 'us-east-1',
identityPoolId: 'us-east-1:xxxx...'
})
Analytics.configure({
AWSPinpoint: {
appId: 'appIdKey',
region: 'us-east-1'
}
})
Analytics.record({ name: 'Hello' })
Storage.configure({
AWSS3: {
bucket: 'bucketname',
region: 'us-east-1'
}
})
Storage.get('s3keyname', { download: true })
.then(result => console.log(result))
.catch(err => console.log(err))
import Auth from '@aws-amplify/auth'
import Storage from '@aws-amplify/storage'
Auth.configure({
region: 'us-east-1',
identityPoolId: 'us-east-1:xxxx...'
})
Storage.configure({
AWSS3: {
bucket: 'bucketname',
region: 'us-east-1'
}
})
Storage.get('s3keyname', { download: true })
.then(result => console.log(result))
.catch(err => console.log(err))
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'
import { CognitoIdentityClient } from '@aws-sdk/client-cognito-identity'
import { fromCognitoIdentityPool } from '@aws-sdk/credential-provider-cognito-identity'
const credentials = fromCognitoIdentityPool({
client: new CognitoIdentityClient({
region: 'us-east-1'
}),
identityPoolId: 'us-east-1:xxxx...'
})
const s3Client = new S3Client({
credentials,
region: 'us-east-1'
})
s3Client.send(new GetObjectCommand({
Bucket: 'bucketname',
Key: 'public/s3keyname'
})).then((data) => console.log(data))
.catch((err) => console.log(err))
// Podfile
// pod 'AWSCognito'
// pod 'AWSS3'
import AWSCognito
import AWSS3
func getS3Object() -> Any {
let cognitoIdentityPool = "us-east-1:xxxx..."
let credentialsProvider:AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(
regionType: .USEast1,
identityPoolId: cognitoIdentityPool
)
let configuration = AWSServiceConfiguration(
region: .USEast1,
credentialsProvider: credentialsProvider
)
AWSServiceManager.default().defaultServiceConfiguration = configuration
let request = AWSS3GetObjectRequest()
request?.bucket = "bucketname"
request?.key = "public/s3keyname"
return AWSS3.default().getObject(request!).continueWith {
(task) -> Any? in
if let error = task.error {
print("Error occurred: \(error)")
return nil
}
if let result = task.result {
print("Result: \(result)")
return result
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment