-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide custom AWS Credentials for storage category #3917
Comments
Hi @oleg-moseyko, thank you for using Amplify! Amplify Storage library manages the tokens retrieved from Cognito to interact with S3 buckets. |
Hi @edisooon |
This has been identified as a feature request. If this feature is important to you, we strongly encourage you to give a 👍 reaction on the request. This helps us prioritize new features most important to you. Thank you! |
@oleg-moseyko Providing custom AWS Credentials is currently not supported in the Storage category. |
.. my current state is like on harsh62-avatar. so in 21 century basic developer with basic size mobile application and basic backend to implement basic upload to S3 should:
is this way suggested as the best and simplest? |
From an Amplify perspective, this is considered as an advanced and not very common use case. This is the first feature request we have received for providing custom credentials to storage category. You could still use the AWS SDK for Swift or Kotlin to achieve your use case, just not using Amplify. Ideal use case would be:
|
@harsh62
how to live with this fucking nonsense?
|
Thanks for raising this concern. I understand the frustration when navigating through the complexities of AWS Services and available features. Let’s address your requirements step-by-step. Specifically, I’ll focus on federating into Cognito Identity Pools using a custom backend and developer-provided identity IDs. I think the following image best reflects your architecture and probably how you should setup everything to access backend resources securely.
Using Developer Authenticated IdentitiesTo implement developer-authenticated identities, the flow involves:
Below are examples for the backend (in PHP) and the frontend. Backend Example (PHP)Using the AWS SDK for PHP, your backend can generate tokens by calling My PHP skills are not very polished, but this is what I could come up with a little help from Amazon Q <?php
require 'vendor/autoload.php';
use Aws\CognitoIdentity\CognitoIdentityClient;
use Aws\Exception\AwsException;
class CognitoService
{
private $client;
public function __construct($region)
{
$this->client = new CognitoIdentityClient([
'version' => 'latest',
'region' => $region,
]);
}
public function getIdentityToken($identityPoolId, $providerName, $userId)
{
try {
$result = $this->client->getOpenIdTokenForDeveloperIdentity([
'IdentityPoolId' => $identityPoolId,
'Logins' => [
$providerName => $userId,
],
'TokenDuration' => 86400, // Token duration in seconds (optional)
]);
return [
'identityId' => $result['IdentityId'],
'token' => $result['Token'],
];
} catch (AwsException $e) {
throw new Exception('Error generating token: ' . $e->getMessage());
}
}
}
// Usage
$cognitoService = new CognitoService('us-east-1');
$identityPoolId = 'your-identity-pool-id';
$providerName = 'your-auth-provider-name';
$userId = 'unique-user-identifier';
try {
$credentials = $cognitoService->getIdentityToken($identityPoolId, $providerName, $userId);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
} This script:
Frontend Example (Swift)On the frontend, use the import Amplify
func federateToIdentityPoolsUsingCustomIdentityId(identityId: String, token: String) async throws {
guard let authCognitoPlugin = try Amplify.Auth.getPlugin(for: "awsCognitoAuthPlugin") as? AWSCognitoAuthPlugin else {
fatalError("Unable to get the Auth plugin")
}
do {
let result = try await authCognitoPlugin.federateToIdentityPool(
withProviderToken: token,
for: .custom("your-auth-provider-name"),
options: .init(developerProvidedIdentityID: identityId)
)
print("Federation successful with result: \(result)")
} catch {
print("Failed to federate to identity pools with error: \(error)")
}
} StorageOnce you have completed the above steps, you should be able to use the Storage without any additional configuration, as Amplify under the hood will use the developer provided identity id and token to generate AWS Credentials on your behalf. Key Considerations
Further ReadingFor more details on Developer Authenticated Identities and how they work, check out this blog: If you run into specific issues during implementation, feel free to share more details—I’m happy to help! Hopefully this should enough to get you over the finish line. 🤞 |
Hi @harsh62 |
Is your feature request related to a problem? Please describe.
I would like to upload file to bucket but I can't figure out how to set up Amplify configure by accessKeyId/secretKey with sessionToken which i get from my server:
The text was updated successfully, but these errors were encountered: