简体   繁体   中英

How to set UserAgent while uploading a file to AWS S3 in C# (AWSSDK.net)

I would like to set UserAgent while uploading a file to Amazon S3 bucket. In the older version of AWS sdk there is property in ClientConfig where in we can set or get the UserAgent. But in the latest version it changed to get. So, how can i set this option in my application.

Programing Language: C#

SDK Version: Latest (3.3.5.2)

Environment: VS-2015 and Windows.

We also had similar requirement. We were able to solve it using reflection. Please note that this not a portable solution and it may break if you upgrade the AWS SDK library

        AmazonS3Config config = new AmazonS3Config();
        var bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
        var fields= config.GetType().GetFields(bindingFlags);
        var userAgent = fields.Where(x => x.Equals("_userAgent") && x.FieldType == typeof(string)).First();
        if (userAgent != null)
        {
            userAgent.SetValue(config, "my-custom-user-agent");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM