aws-sdk-js-v3: TypeError: readableStream.tee is not a function (it is undefined)
Checkboxes for prior research
- I’ve gone through Developer Guide and API reference
- I’ve checked AWS Forums and StackOverflow.
- I’ve searched for previous similar issues and didn’t find any solution.
Describe the bug
I am trying to implement chunk approach to upload files in react native , if the file size is less then 5 mb its working fine other wise i am getting this error
SDK version number
“@aws-sdk/client-s3”: “^3.592.0”, “@aws-sdk/lib-storage”: “^3.620.0”,
Which JavaScript Runtime is this issue in?
React Native
Details of the browser/Node.js/ReactNative version
“react-native”: “0.74.1”,
Reproduction Steps
index.js
// import 'react-native-url-polyfill/auto';
// import 'react-native-get-random-values';
// import {ReadableStream} from 'web-streams-polyfill'
// globalThis.ReadableStream = ReadableStream;
import 'react-native-polyfill-globals/auto'
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);
import {PutObjectCommand, S3Client, S3ClientConfig} from '@aws-sdk/client-s3';
import Config from 'react-native-config';
import RNFS from 'react-native-fs';
import {Buffer} from 'buffer';
import {handleS3Upload} from '../store/slices/homeSlice/homeSlice';
import {Audio, getFileSize, Image, Video} from 'react-native-compressor';
import {Upload} from '@aws-sdk/lib-storage';
const compressFileBasedOnType = async (
filePath: string,
contentType: string,
): Promise<string> => {
let compressedFilePath = filePath;
if (contentType.startsWith('image/')) {
compressedFilePath = await Image.compress(filePath, {
compressionMethod: 'auto',
});
} else if (contentType.startsWith('video/')) {
compressedFilePath = await Video.compress(filePath, {
compressionMethod: 'auto',
});
} else if (contentType.startsWith('audio/')) {
compressedFilePath = await Audio.compress(filePath, {
quality: 'medium',
});
} else {
compressedFilePath = filePath;
}
return compressedFilePath;
};
interface UploadParams {
filePath: string;
fileName: string;
contentType: string;
dispatch?: (action: any) => void;
}
const createS3Client = (): S3Client => {
const accessKeyId = Config.ACCESS_KEY_S3;
const secretAccessKey = Config.SECRET_KEY_S3;
if (!accessKeyId || !secretAccessKey) {
throw new Error('AWS S3 credentials are not set');
}
const s3Config: S3ClientConfig = {
region: Config.S3_BUCKET_REGION,
credentials: {
accessKeyId,
secretAccessKey,
},
};
return new S3Client(s3Config);
};
const uploadFileToS3 = async ({
filePath,
fileName,
contentType,
dispatch,
}: UploadParams): Promise<string> => {
try {
if (dispatch) {
dispatch(handleS3Upload(true));
}
const compressedFilePath = await compressFileBasedOnType(
filePath,
contentType,
);
const fileContent = await RNFS.readFile(compressedFilePath, 'base64');
const fileSize = await getFileSize(compressedFilePath);
console.log('FileSize ====>', fileSize);
const fileBuffer = Buffer.from(fileContent, 'base64');
const s3Client = createS3Client();
// const command = new PutObjectCommand({
const upload = new Upload({
client: s3Client,
params: {
Bucket: Config.BUCKET_NAME_S3,
Key: fileName,
Body: fileBuffer,
ContentType: contentType,
},
partSize: 5 * 1024 * 1024, // 5 MB
queueSize: 4, // 4 concurrent uploads
leavePartsOnError: false, // Cleanup parts on error
});
upload.on('httpUploadProgress', progress => {
console.log('Upload Progress:', progress);
// You can also update your app's UI with the progress information here
});
await upload.done();
if (dispatch) {
dispatch(handleS3Upload(false));
}
const fileUrl = `https://${Config.BUCKET_NAME_S3}.s3.${Config.S3_BUCKET_REGION}.amazonaws.com/${fileName}`;
console.log('fileUrl ======>', fileUrl);
return fileUrl;
} catch (error) {
console.log('Error', error?.message, error);
if (dispatch) {
dispatch(handleS3Upload(false));
}
throw error;
}
};
export default uploadFileToS3;
Observed Behavior
TypeError: readableStream.tee is not a function (it is undefined)
Expected Behavior
It should have to upload images , videos in chunk if its more then 5mb in size
Possible Solution
No response
Additional Information/Context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (6 by maintainers)
Hey all - thanks everyone for reporting. I was able to reproduce the error reported. For further analysis and potential resolution, I’ve prepared a couple test cases along with the versions of the relevant dependencies used during our testing.
ListObjectsandPutObjectwith S3ListObjectsandPutObject:TypeError: readableStream.tee is not a function (it is undefined)web-streams-polyfilland error remains.npm i @aws-sdk/client-s3@3.574.0ListTablesandPutItemwith DynamoDBNo error thrown for both operations.
If you’re encountering the reported error with S3, please downgrade to
@aws-sdk/client-s3@3.574.0as a workaround, while we further look into it.Finally found a solution! Thank you so much!
@aBurmeseDev I am still facing issues with this workaround sometimes it works fine and sometimes it still gives this error [TypeError: readableStream.tee is not a function (it is undefined)]