简体   繁体   中英

StitchServiceError “aws: ”aws_service“ is a required string”, errorCodeName: InvalidParameter

I'm setting up AWS S3 bucket to upload audio files to using MongoDB Stitch (here are the docs mongo s3 docs . After following the instructions and authenticating my user I keep geting this error when trying to upload the selected file: error image from console

On line 119 where the error is coming from I'm just catching the error after running AWS build:

const aws = stitchClient.getServiceClient(AwsServiceClient.factory, "AWS");

convertAudioToBSONBinaryObject(file).then((result) => {
  const audiofile = mongodb.db("data").collection("audiofile");
  //now we need an instance of AWS service client
  const key = `${stitchClient.auth.user.id}-${file.name}`;
  // const key = `${stitchClient.auth.user.id}-${file.name}`;
  const bucket = "myBucketName";
  const url =
    "http://" + bucket + ".s3.amazonaws.com/" + encodeURIComponent(key);

  const args = {
    ACL: "public-read",
    Bucket: bucket,
    ContentType: file.type,
    Key: key,
    Body: result,
    // aws_service: "s3",
  };
  // building the request
  const request = new AwsRequest.Builder()
    .withService("s3")
    .withAction("PutObject")
    .withRegion("us-east-1")
    .withArgs(args);

  aws
    .execute(request.build)
    .then((result) => {
      console.log(result);
      console.log(url);
      return audiofile.insertOne({
        owner_id: stitchClient.auth.user.id,
        url,
        file: {
          name: file.name,
          type: file.type,
        },
        Etag: result.Etag,
        ts: new Date(),
      });
    })
    .then((result) => {
      console.log("last result", result);
    })
    .catch((err) => {
      console.log(err);
    });
});

My Stitch rule for s3 looks like this: Stitch rule for AWS s3

So it seems to me that everything is set up the way it's inteded to, but the error tells me I'm not passing all the needed args. I'd really appreciate any thoughts on how to fix this error.

PS If I change "AWS" to "AWS_S3" in this line: const aws = stitchClient.getServiceClient(AwsServiceClient.factory, "AWS"); The error message changes to this:

StitchServiceError {message: "service not found: 'AWS_S3'", name: "StitchServiceError", errorCode: 18, errorCodeName: "ServiceNotFound",

And the log in Stitch shows this for information for both errors: Stitch Logs

The answer to this is a simple typo in this line:

aws.execute(request.build).then((result)

build is a function so I just needed to call it - (request.build()).then((result). Issue solved, thanks all!

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