Shafi ul Hussnain
Shafi ul Hussnain

Reputation: 1

Upload File using PUT request to a Signed URL

I am trying to send a file to a signed URL using PUT request but the file is not uploading. Below is my code:

const uploadToBucket = async (uploadUrl, fileUri, contentType, fileSize) => {
    try {
      const fileResponse = await fetch(fileUri);
      const blob = await fileResponse.blob(); 
      const response = await axios.put(uploadUrl, blob, {
        headers: {
          'Content-Type': contentType,
          'x-goog-content-length-range': `0,${fileSize}`,
        },
      });

      // 3. Check response status
      if (response.status == 200 ) {
        console.log('File uploaded successfully:', response.data);
      } else {
        console.error(`Upload failed: ${response.status} - ${response.data}`);
        Alert.alert('Upload Error', 'Failed to upload file. Please try again.');
      }
    } catch (error) {
      console.error('Error while uploading file:', error);
      Alert.alert('Error', 'Something went wrong during the file upload.');
    }
  };

I also tried sending file as a formData but it does not work either. rnfs.readFile() I also tried but It cannot not read big files.

Upvotes: 0

Views: 13

Answers (0)

Related Questions