Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error when trying to upload image to s3 bucket #87

Closed
mattRiddoch opened this issue Aug 27, 2020 · 15 comments
Closed

Fatal error when trying to upload image to s3 bucket #87

mattRiddoch opened this issue Aug 27, 2020 · 15 comments
Labels
storage Issues related to the Storage Category

Comments

@mattRiddoch
Copy link

Describe the bug
Unexpectedly found nil while implicitly unwrapping an Optional value: file..../ios/Pods/AmplifyPlugins/AmplifyPlugins/Storage/AWSS3StoragePlugin/AWSS3StoragePlugin+ClientBehavior.swift, line 157
passing the file from the imagepicker
upload snippet:

await picker
   .getImage(source: ImageSource.gallery)
    .then((value) async {
        File fileInstance = File(value.path);
        final key = new DateTime.now().toString();
        UploadFileResult result =
            await Amplify.Storage.uploadFile(
                 key: key, local: fileInstance);
       });

crashing swift snippet (crashes on storageService):

let uploadFileOperation = AWSS3StorageUploadFileOperation(request,
                                                                  storageService: storageService,
                                                                  authService: authService,
                                                                  progressListener: progressListener,
                                                                  resultListener: resultListener)

I have an authenticated user.

To Reproduce
Steps to reproduce the behavior:

  1. Pass key and file to Amplify.Storage.uploadFile
  2. Raise exception in xcode

Expected behavior
I expect the file to upload.

Screenshots
If applicable, add screenshots to help explain your problem.

Platform
Amplify Flutter current supports iOS and Android. This issue is reproducable in (check all that apply):
[] Android
[x] iOS

Smartphone (please complete the following information):

  • Device: simulator 11 pro
  • OS: 13.6
@Ashish-Nanda Ashish-Nanda added storage Issues related to the Storage Category to-be-reproduced Issues that have not been reproduced yet, but have reproduction steps provided labels Aug 27, 2020
@Ashish-Nanda
Copy link
Contributor

Ashish-Nanda commented Aug 28, 2020

@mattRiddoch can you confirm that value.path is indeed a valid file path before passing it to the uploadFile() API?

@mattRiddoch
Copy link
Author

@Ashish-Nanda Here is what the path looks like:
/Users/mattriddoch/Library/Developer/CoreSimulator/Devices/F694C06D-4AEA-490F-BE99-CB8635D94192/data/Containers/Data/Application/2F52DFEB-FD29-4F4E-A364-EEB065717896/tmp/image_picker_26878D92-6B72-499F-BB77-8DE1FFF011FC-52750-0000DACF01486E63.jpg
The image is coming from the library via image_picker: ^0.6.7+4 in the simulator.

@fjnoyp
Copy link
Contributor

fjnoyp commented Aug 31, 2020

Hi @mattRiddoch

I think the issue is likely coming from the image_picker library you're using. I've tried reproducing your error (using a different picker library) and have no issues uploading image files from my gallery on a iPhone SE emulator.

Could you try using the file_picker: ^1.8.0+1 in your pubspec.yaml instead to see if that works for you? We use that in our example app for picking files to upload.

@Ashish-Nanda
Copy link
Contributor

Ashish-Nanda commented Sep 1, 2020

@mattRiddoch

The path of the file you provided seems fine, so not sure why you are getting the error. I also tried a clean setup today in an attempt to reproduce your issue and can confirm upload is working as expected. We are using the file_picker library @fjnoyp mentioned above. Could you give that a try based on the code in our sample app?

Let us know if you still face issues.

@mattRiddoch
Copy link
Author

mattRiddoch commented Sep 2, 2020

OK! I brought in the file_picker library and am using the following uploading snippet from the example code:

void upload() async {
    try {
      print('In upload');
      // Uploading the file with options
      File local = await FilePicker.getFile(type: FileType.image);
      final key = new DateTime.now().toString();
      Map<String, String> metadata = <String, String>{};
      metadata['name'] = 'filename';
      metadata['desc'] = 'A test file';

      S3UploadFileOptions options = S3UploadFileOptions(
          accessLevel: StorageAccessLevel.guest, metadata: metadata);
      UploadFileResult result = await Amplify.Storage.uploadFile(
          key: key, local: local, options: options);
        print(result.key);
    } catch (e) {
      print('UploadFile Err: ' + e.toString());
    }
  }

Upon picking an asset I get the following error:
UploadFile Err: RangeError (index): Invalid value: Valid value range is empty: 0
I did set a breakpoint prior to the upload attempt and can confirm that a valid file is being passed in?

On a side note this is the exact same error I raise when I save a file to the documents folder and attempt to upload from there as well?

@fjnoyp
Copy link
Contributor

fjnoyp commented Sep 4, 2020

Hi @mattRiddoch it's strange the example code isn't working for you now as well. We've validated this code works so this issue could be related to your configuration.

Have you tried running the Amplify CLI to create a s3 bucket and performed an upload. Can you provide details on how you've generated your amplifyconfiguration.dart file?

@Ashish-Nanda
Copy link
Contributor

@mattRiddoch can you share you amplifyconfiguration.dart file?

@mattRiddoch
Copy link
Author

Sure thing @Ashish-Nanda

const amplifyconfig = ''' {
    "UserAgent": "aws-amplify-cli/2.0",
    "Version": "1.0",
    "auth": {
        "plugins": {
            "awsCognitoAuthPlugin": {
                "UserAgent": "aws-amplify-cli/0.1.0",
                "Version": "0.1.0",
                "IdentityManager": {
                    "Default": {}
                },
                "CredentialsProvider": {
                    "CognitoIdentity": {
                        "Default": {
                            "PoolId": "<PoolId>
                            "Region": "us-east-1"
                        }
                    }
                },
                "CognitoUserPool": {
                    "Default": {
                        "PoolId": "<PoolId>",
                        "AppClientId": "<ClientId>",
                        "AppClientSecret": "<ClientSecret>",
                        "Region": "us-east-1"
                    }
                },
                "Auth": {
                    "Default": {
                        "authenticationFlowType": "USER_SRP_AUTH"
                    }
                },
                "PinpointAnalytics": {
                    "Default": {
                        "AppId": "<AppId>",
                        "Region": "us-east-1"
                    }
                },
                "PinpointTargeting": {
                    "Default": {
                        "Region": "us-east-1"
                    }
                },
                "S3TransferUtility": {
                    "Default": {
                        "Bucket": "<BucketId>
                        "Region": "us-east-1"
                    }
                }
            }
        }
    },
    "analytics": {
        "plugins": {
            "awsPinpointAnalyticsPlugin": {
                "pinpointAnalytics": {
                    "appId": "<AppId>",
                    "region": "us-east-1"
                },
                "pinpointTargeting": {
                    "region": "us-east-1"
                }
            }
        }
    },
    "storage": {
        "plugins": {
            "awsS3StoragePlugin": {
                "bucket": "<bucketId>",
                "region": "us-east-1",
                "defaultAccessLevel": "guest"
            }
        }
    }
}''';

@adamcyber1
Copy link

I am having an issue identical to this one - I am configured using the example Storage application and upon uploading I also get UploadFile Err: RangeError (index): Invalid value: Valid value range is empty: 0

@Shrey29ansh
Copy link

You can try first getting a path of the file and store that in some variable then pass that value in uploadFile function like this
image

Hope it helps!

@mattRiddoch
Copy link
Author

@Shrey29ansh I will give this a try soon. I have gotten a little sidetracked with other features but this is still very much an issue for me. Much appreciated.

@fjnoyp
Copy link
Contributor

fjnoyp commented Oct 2, 2020

Hey @mattRiddoch have you been able to resolve this issue?

@naoyashiga
Copy link

I also got this error.

RangeError (index): Invalid value: Valid value range is empty: 0.

Now I have solved this error.

The reason was that the list "plugins" in the StorageCategory class was empty.

The problem is probably in the initial configuration of the plugins.

Here is the code before the fix.

// Add Pinpoint and Cognito Plugins
AmplifyAnalyticsPinpoint analyticsPlugin = AmplifyAnalyticsPinpoint();
AmplifyAuthCognito authPlugin = AmplifyAuthCognito();
amplifyInstance.addPlugin(authPlugins: [authPlugin]);
amplifyInstance.addPlugin(analyticsPlugins: [analyticsPlugin]);

AmplifyDataStore datastorePlugin =
AmplifyDataStore(modelProvider: ModelProvider.instance);
amplifyInstance.addPlugin(dataStorePlugins: [datastorePlugin]);

// Add this line, to include the Auth plugin.
AmplifyStorageS3 storage = AmplifyStorageS3();
amplifyInstance.addPlugin(
    authPlugins: [authPlugin],
    storagePlugins: [storage]
);

// Once Plugins are added, configure Amplify
await amplifyInstance.configure(amplifyconfig);

Here is the code after the fix.

// Add Pinpoint and Cognito Plugins
AmplifyAnalyticsPinpoint analyticsPlugin = AmplifyAnalyticsPinpoint();
AmplifyAuthCognito authPlugin = AmplifyAuthCognito();

AmplifyDataStore datastorePlugin =
AmplifyDataStore(modelProvider: ModelProvider.instance);

// Add this line, to include the Auth plugin.
AmplifyStorageS3 storage = AmplifyStorageS3();
amplifyInstance.addPlugin(
    authPlugins: [authPlugin],
    analyticsPlugins: [analyticsPlugin],
    dataStorePlugins: [datastorePlugin],
    storagePlugins: [storage]
);

// Once Plugins are added, configure Amplify
await amplifyInstance.configure(amplifyconfig);

@offlineprogrammer offlineprogrammer added pending-close-response-required The issue will be closed if details necessary to reproduce the issue are not provided within 7 days. and removed to-be-reproduced Issues that have not been reproduced yet, but have reproduction steps provided labels Apr 30, 2021
@offlineprogrammer
Copy link
Contributor

Hey @mattRiddoch . Sorry for the late reply.

I tested in iOS, and Android and I couldn't reproduce the issue

Feel free to test using the latest build and let us know if you got any issues

Thanks

@offlineprogrammer
Copy link
Contributor

Hey @mattRiddoch

I am closing this issue for now as I didn't hear from you and I couldn't reproduce as per my comment above.
We can reopen it if you are still facing the issue and when you provide details

Regards
Mo

@offlineprogrammer offlineprogrammer removed the pending-close-response-required The issue will be closed if details necessary to reproduce the issue are not provided within 7 days. label May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
storage Issues related to the Storage Category
Projects
None yet
Development

No branches or pull requests

7 participants