aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.12k stars 578 forks source link

TypeError: (0, import_protocols.resolvedPath) is not a function error when using release build in android #6656

Open saifalitai opened 1 day ago

saifalitai commented 1 day ago

Checkboxes for prior research

Describe the bug

I am encountering an error when trying to use the @aws-sdk/client-s3 package within a React Native project. The error seems to occur due to a missing or incompatible function import_protocols.resolvedPath. This issue prevents me from successfully uploading files to S3 from my React Native application.

### Code Sample:

import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import RNFS from "react-native-fs";

const uploadFileToS3 = async (filePath) => {
  const s3Keys = {
    region: "us-west-2",
    s3Bucket: "my-bucket",
    key: "YOUR_ACCESS_KEY",
    sKey: "YOUR_SECRET_KEY",
  };
  const s3Client = new S3Client({
    region: s3Keys.region,
    credentials: {
      accessKeyId: s3Keys.key,
      secretAccessKey: s3Keys.sKey,
    },
  });

  const content = await RNFS.readFile(filePath, 'ascii');
  const uploadParams = {
    Bucket: s3Keys.s3Bucket,
    Key: "path/to/file",
    Body: content,
    ContentType: "application/octet-stream",
  };

  try {
    const command = new PutObjectCommand(uploadParams);
    await s3Client.send(command);
  } catch (error) {
    console.error("Upload error:", error);
  }
};

Regression Issue

SDK version number

@aws-sdk/package-name@version, ...

Which JavaScript Runtime is this issue in?

React Native

Details of the browser/Node.js/ReactNative version

React Native version: 0.66.5 AWS SDK version: @aws-sdk/client-s3 (version: "3.688.0") Node version: v20.11.0 Platform: Android (34) react: "18.2.0",

Reproduction Steps

  1. Install @aws-sdk/client-s3 in a React Native project.
  2. Configure the S3 client and attempt to upload a file.
  3. Observe the TypeError related to import_protocols.resolvedPath.

Observed Behavior

Here is the exact error message:

TypeError: (0, import_protocols.resolvedPath) is not a function. (In '(0, import_protocols.resolvedPath)(path, _this2.input, memberName, labelValueProvider, uriLabel, isGreedyLabel)', '(0, import_protocols.resolvedPath)' is undefined) at line: 146395, column: 59, sourceURL: 'index.android.bundle'

Expected Behavior

The file should be successfully uploaded to S3 without throwing any errors.

Possible Solution

Updated @aws-sdk/client-s3 to the latest version. Cleared npm cache and reset React Native bundler cache. Tried alternative S3 libraries, but prefer to use @aws-sdk/client-s3. Additional Context: This error appears specifically related to React Native, as similar code works fine in a Node.js environment.

Special not is working debug build on @aws-sdk/client-s3 (version: "3.609.0"). but not worked on release build

Additional Information/Context

Could you please provide guidance on resolving this or recommend a workaround? Thank you for your assistance!

dyarfaradj commented 1 day ago

Same issue but with node.js

princekurt commented 22 hours ago

https://github.com/aws/aws-sdk-js-v3/issues/6626

Reported already here!

zshzbh commented 20 hours ago

Hey,

Could you please confirm that you have already added additional packages for react native project?

If you are consuming modular AWS SDK for JavaScript on react-native environments, you will need to add and import following polyfills in your react-native application:

react-native-get-random-values react-native-url-polyfill web-streams-polyfill

import "react-native-get-random-values";
import "react-native-url-polyfill/auto";
import "web-streams-polyfill/dist/polyfill";

import { DynamoDB } from "@aws-sdk/client-dynamodb";

Specifically Metro bundler used by react-native, enable Package Exports Support:

https://metrobundler.dev/docs/package-exports/ https://reactnative.dev/blog/2023/06/21/package-exports-support

zshzbh commented 19 hours ago

Hey @dyarfaradj,

May I know how do you access file system in node.js? I tried to use fs but didn't see any issues.

import {
  S3Client,
  PutObjectCommand,
  CopyObjectCommand
} from "@aws-sdk/client-s3";
// import RNFS from "react-native-fs";
import fs from 'fs/promises';
// Create an S3 client
const config = {
  region: "us-east-1",
  credentials: {
    accessKeyId: "XXX",
    secretAccessKey: "XXXX",
  },
};
const s3 = new S3Client(config);

//const content = await fs.readFile('./test.txt');

s3.send(new PutObjectCommand({
    Bucket: "new-bucket-XXX",
    Key: `test_RNFS.txt`,
    Body: content,
  }));

And I do see the file has been uploaded to s3 bucket.

dyarfaradj commented 4 hours ago

Hey @dyarfaradj,

May I know how do you access file system in node.js? I tried to use fs but didn't see any issues.

import {
  S3Client,
  PutObjectCommand,
  CopyObjectCommand
} from "@aws-sdk/client-s3";
// import RNFS from "react-native-fs";
import fs from 'fs/promises';
// Create an S3 client
const config = {
  region: "us-east-1",
  credentials: {
    accessKeyId: "XXX",
    secretAccessKey: "XXXX",
  },
};
const s3 = new S3Client(config);

//const content = await fs.readFile('./test.txt');

s3.send(new PutObjectCommand({
  Bucket: "new-bucket-XXX",
  Key: `test_RNFS.txt`,
  Body: content,
  }));

And I do see the file has been uploaded to s3 bucket.

I use it like this:

var s3params = {
  Bucket: 'blabla-YYYY',
  Key: 'test.zip',
  Body: fs.createReadStream(`${process.cwd()}/.build/test.zip`),
};
s3.putObject(s3params, function (err) {
  if (err) {
    console.error('Error uploading to S3:', err);
    reject(new Error(`Uploading to S3 failed: ${err.message}`));
  } else {
    resolve();
  }
});
JulianHillworth commented 1 hour ago

Ok team, after spending about 5 hours on this and scowling through multiple package-lock.json files on multiple git histories I uncovered root cause. @smithy/core (sub dependency) an internal library for the AWS SDK updated itself to 2.5.1. from 2.4.8

Workaround until AWS fix.

In your package.json dependencies just put "@smithy/core": "2.4.8" to enforce the last known working version. Error disappears and uploading to s3 recommenced!