benjreinhart / react-native-aws3

Pure JavaScript React Native library for uploading to AWS S3
MIT License
399 stars 151 forks source link

Image 0 bytes when uploaded to S3 #31

Closed AdamJNavarro closed 7 years ago

AdamJNavarro commented 7 years ago

Was wondering if anyone else had encountered this issue and perhaps had some feedback on how it got resolved. Full disclosure, I'm pretty new to programming so this very well could be a trivial error on my part. Additionally, it may not have anything to do with the library in which case I apologize. Running on the newest version(0.0.4). I logged the results and everything seems to be in order but when I go to the bucket, the file is 0 bytes and nothing is visible. Below is all what I believe to be pertinent code from my project. Any help would be much appreciated!

// Camera Component

@connectActionSheet
class PicturePicker extends React.Component {
  state = {
    image: null,
  }

  render() {
    let { image } = this.state;

    return (
      <View>
      <TouchableNativeFeedbackSafe
        style={styles.profilePictureContainer}
        delayPressIn={150}
        onPress={this._editProfilePicture}>
          <FadeIn placeholderStyle={{backgroundColor: Platform.OS === 'android' ? 'transparent' : 'transparent' }}>
            <View style={styles.profilePicture}>
            { image && <Image
                source={{uri: image }}
                style={styles.profilePicture}
              /> }
            </View>
          </FadeIn>
      </TouchableNativeFeedbackSafe>
      </View>
    )
  };

   _editProfilePicture = () => {
    this.props.showActionSheetWithOptions({
        options: [ 'Upload Image', 'Take Picture', 'Cancel'],
        cancelButtonIndex: 2,
      },
      (buttonIndex) => {
        if (buttonIndex === 1) {
          this._openCamera();
        } else if (buttonIndex === 0) {
          this._openGallery();
        }
      },
    )};

    _openGallery = async () => {
      let result = await Exponent.ImagePicker.launchImageLibraryAsync({
        allowsEditing: true,
      });

      if (!result.cancelled) {
        const image = result.uri;
        this.setState({image: result.uri});
        this.props.imageChanged(image);
        console.log(this.props.imageChanged(image));
      }
    };

    _openCamera = async () => {
      let result = await Exponent.ImagePicker.launchCameraAsync({
        allowsEditing: true,
      });

      if (!result.cancelled) {
        const image = result.uri;
        this.setState({image: result.uri});
        this.props.imageChanged(image);
      }
    };
}
// Redux Actions 
export const imageChanged = (image) => {
  console.log(image)
  return {
    type: IMAGE_CHANGED,
    payload: image,
  };
};

export const imageUploaded = (image) => {
  return (dispatch) => {

  const timeStamp = new Date().getTime();

  const file = {
    uri: image,
    name: `profilePicture${timeStamp}.jpg`,
    type: 'image/jpg'
  };

  const options = {
    keyPrefix: "profilePictures/",
    bucket: "BUCKET-NAME",
    region: "us-east-1",
    accessKey: "ACCESSKEY",
    secretKey: "SECRETKEY",
    successActionStatus: 201,
  };

  RNS3.put(file, options).then(response => {
    if (response.status !== 201) {
      throw new Error('Failed to upload image to S3', response);
    }
    console.log(response.body)
    const picture = response.body.postResponse.location;
    firebaseImage(picture, dispatch);
  });
 };
};
// Redux Reducers

import {
  USERNAME_CHANGED,
  USERNAME_REGISTERED,
  DISPLAY1_CHANGED,
  DISPLAY2_CHANGED,
  DISPLAY_REGISTERED,
  IMAGE_CHANGED,
  IMAGE_UPLOADED,
} from '../actions/actionTypes';

const INITIAL_STATE = {
  username: '',
  display1: '',
  display2: '',
  image: '',
  error: '',
  loading: false,
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case USERNAME_CHANGED:
      return { ...state, username: action.payload };
    case USERNAME_REGISTERED:
      return { ...state, loading: true, error: ''};
    case DISPLAY1_CHANGED:
      return { ...state, display1: action.payload };
    case DISPLAY2_CHANGED:
      return { ...state, display2: action.payload };
    case DISPLAY_REGISTERED:
      return { ...state, loading: true, error: ''};
    case IMAGE_CHANGED:
      return { ...state, image: action.payload };
    case IMAGE_UPLOADED:
      return { ...state, loading: true, error: ''};
    default:
      return state;
    }
};
// Console Log Results

file:///var/mobile/Containers/Data/Application/E05302D4-E48B-48CD-8770-D8F7BA2E9907/tmp/25471B1E-407B-4FF4-AA1C-04FDD4107623.jpg

{"type":"image_changed","payload":"file:///var/mobile/Containers/Data/Application/E05302D4-E48B-48CD-8770-D8F7BA2E9907/tmp/25471B1E-407B-4FF4-AA1C-04FDD4107623.jpg"}

{"postResponse":{"key":"profilePictures/profilePicture1488246044447.jpg","etag":"d41d8cd98f00b204e9800998ecf8427e","bucket":"BUCKETNAME","location":"https://BUCKETNAME.s3.amazonaws.com/profilePictures%2FprofilePicture1488246044447.jpg"}}
AdamJNavarro commented 7 years ago

Silly mistake on my end. Everything is working fine now.

bucketclan commented 7 years ago

@AdamJNavarro Can you let me know, what was your mistake. I am facing the same problem.