Home  >  Article  >  Backend Development  >  How to Use AWS SDK v2 with Credentials from Variables?

How to Use AWS SDK v2 with Credentials from Variables?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-03 01:25:02635browse

How to Use AWS SDK v2 with Credentials from Variables?

Running AWS SDK v2 with Credentials from Variables

Q: How do I run AWS SDK v2 with credentials from variables?

To leverage SDK v2 without using the legacy Session class, you can create a new client and pass your credentials as variables. Consider this getIAMClient function for the IAM service:

<code class="go">func getIAMClient(ctx context.Context) (*iam.Client, error) {
    cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("no-region"))
    if err != nil {
        return nil, errors.Wrap(err)
    }

    cfg.HTTPClient, err = getHTTPClient(ctx)
    if err != nil {
        return nil, err
    }

    // Use the StaticCredentialsProvider to pass credentials from variables.
    cfg.Credentials = credentials.NewStaticCredentialsProvider("AKID", "SECRET_KEY", "TOKEN")
    return iam.NewFromConfig(cfg), nil
}</code>

This approach allows multiple users to utilize the application simultaneously without relying on environment variables.

The above is the detailed content of How to Use AWS SDK v2 with Credentials from Variables?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn