rust-s3 icon indicating copy to clipboard operation
rust-s3 copied to clipboard

Attempting to load Credentials Results in 'NotEc2' Error

Open amiller68 opened this issue 2 years ago • 6 comments

Describe the bug I am trying to load my default AWS configuration. This keeps generating the following error from my tests:

thread 'tests::test_handler' panicked at 'called Result::unwrap() on an Err value: Not an AWS instance', src/main.rs:100:50

To Reproduce This is the offending line of code:

let credentials = Credentials::default()?;

Expected behavior This should not be generating an error as far as I can tell

Environment

  • Rust version: [e.g. 1.62.1]
  • lib version [e.g. 0.32.1]

Additional context This is the link to the file: https://github.com/banyancomputer/ipfs-proof-oracle/blob/main/src/oracle/backend.rs See line 43 to see how I am calling the code in question.

** ALSO ** Does anyone know any easy way to initialize Region and Credentials as Constants in a file?

amiller68 avatar Jul 26 '22 22:07 amiller68

Ok I got around this by just using my keys as input, but I don't think this error is appropriate

amiller68 avatar Jul 27 '22 14:07 amiller68

@amiller68 thats a good callout, it needs better erorr, it first tries to load credentials, from various places, if none are found it tries to get some from the ec2 instance metadata, but it never tells you what its doing, needs better UX for sure

durch avatar Jul 27 '22 14:07 durch

Yeah I agree. I figured out what was happening from the source code, but like either:

  1. the Credentials struct relies on Ec2 should be documented (not sure if you contribute to awscreds)
  2. or it just shouldn't rely on that lol

I ended up getting to where I needed to be by using hard-coded access keys (not ideal but I can probably work something out with env vars)

@durch do you know a good way to load creds as some sort of static/constant struct? I'm new to rust and keep getting compiler errors :(

amiller68 avatar Jul 27 '22 15:07 amiller68

I'd suggest defining them in the environment of where you're running this, that way you don't need to code anything in

durch avatar Jul 27 '22 17:07 durch

@durch, I was referring to when you eventually have to reference a awscreds::Credentials instance when initializing a new s3 client. I almost found a solution with lazy_static, but I found I still have to clone the Static struct before passing it to bucket::new.

amiller68 avatar Jul 27 '22 17:07 amiller68

You should be able to do Credentials::default(), and it would pick them up from the environment.

let bucket = Bucket::new(
  "test-bucket",
  "eu-central-1".parse().unwrap(),
  &Credentials::default()).unwrap();

^ Not sure if bucket takes a reference or value

durch avatar Jul 27 '22 17:07 durch