Elijah23Johnson / react-native-azure-blob-storage

File Storage In Azure
8 stars 31 forks source link

Save blob with specific name? #1

Closed sakhmedbayev closed 3 years ago

sakhmedbayev commented 4 years ago

Thank you for this module. Great job!

I wonder if there is a way to save a blob using a predefined name, e.g. "hello-world"?

Elijah23Johnson commented 4 years ago

There is surely a way to do this. It would require that you pass in the name of the file to the native modules. And use that instead of the random generated string. I am willing to take merge requests on the project. I just don't have the band width to do it right now.

IOS

[blobContainer createContainerIfNotExistsWithAccessType:AZSContainerPublicAccessTypeContainer requestOptions:nil operationContext:nil completionHandler:^(NSError *error, BOOL exists)
        {
            if (error){
                reject(@"no_event",@"Error in creating container.",error);
            }
            else{
                // Create a local blob object
                NSString ***fileName** = [self genRandStringLength: 10];
                AZSCloudBlockBlob *blockBlob = [blobContainer blockBlobReferenceFromName:**fileName**];
                blockBlob.properties.contentType = @"image/png";
                [blockBlob uploadFromFileWithPath:file  completionHandler:^(NSError * error) {
                    if (error){
                        reject(@"no_event",[NSString stringWithFormat: @"Error in creating blob. %@",file],error);
                    }else{
                        resolve(fileName);
                    }
                }];
            }
        }];

Android

 @ReactMethod
    public void uploadFile(String name, final Promise promise){
        try {
            String file = name.contains("file://") ? name : "file://".concat(name);
            final InputStream imageStream = ctx.getContentResolver().openInputStream(Uri.parse(file));
            final int imageLength = imageStream.available();

            final Handler handler = new Handler();

            Thread th = new Thread(new Runnable() {
                public void run() {

                    try {

                        final String imageName = ImageManager.UploadImage(imageStream, imageLength);

                        handler.post(new Runnable() {

                            public void run() {
                                Toast.makeText(ctx, "Image Uploaded Successfully...", Toast.LENGTH_SHORT).show();
                                promise.resolve(imageName);
                            }
                        });
                    }
                    catch(final Exception ex) {
                        final String exceptionMessage = ex.getMessage();
                        handler.post(new Runnable() {
                            public void run() {
                                Toast.makeText(ctx, exceptionMessage, Toast.LENGTH_SHORT).show();
                                promise.reject(E_LAYOUT_ERROR, ex);
                            }
                        });
                    }
                }});
            th.start();
        }
        catch(Exception ex) {
            Toast.makeText(ctx, ex.getMessage(), Toast.LENGTH_SHORT).show();
             promise.reject(E_LAYOUT_ERROR, ex);
        }
    }

Note for android the name is inferred from the image file name so you will have to get the file out and change the name before you sent it to azure.

NagarajBD commented 4 years ago

hi, i am using this npm react-native-azure-blob-storage, so i am getting error like Container File Name Error [Error: Error in creating container.], please help me, thanks

imsam67 commented 4 years ago

This is a very cool and useful solution. As they say, no good deed ever goes unpunished. So, here's another suggestion:

I think being able to set the name of the blob would be a very useful feature. Also, it would be nice to set content-type as well. Currently, images are uploaded as image/png which works fine for JPG images too but would be nice to set them to image/jpeg or the appropriate content-type of the uploaded file.

Elijah23Johnson commented 3 years ago

Please download latest version...