PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label azure-blob-storage. Show all posts
Showing posts with label azure-blob-storage. Show all posts

Thursday, August 25, 2022

[FIXED] How to access appsettings.json from module Configure Services

 August 25, 2022     appsettings, azure-blob-storage, blazor, connection-string, module     No comments   

Issue

I have a blazor server side solution that contains an appsettings.json

I am configuring the blob storage in the ConfigureServices override in the Application Module of the Applications project. It currently has a hard coded connection string and is working perfectly.

Now I want to move the connection string to the appsettings.json file that is in the Blazor project.

I've tried to inject the IConfiguration into the constructor of the ApplicationModule, but the app throws an error when I try to do so.

I've searched through the ServiceConfigurationContext passed into to the ConfigureServices override. There is a Service property containing a collection of around 1,024 ServiceDescriptors and found one that contains the word IConfiguration in the ServiceType.FullName but haven't been able to figure out how to use it to get at the service itself in order to get at the appsettings.json values.

Can anyone shed any light on how to access the appsettings.json values from the application module?

Here is my code I am working with

namespace MyApp
{
    [DependsOn(
        typeof(MyAppDomainModule),
        typeof(AbpAccountApplicationModule),
        typeof(MyAppApplicationContractsModule),
        typeof(AbpIdentityApplicationModule),
        typeof(AbpPermissionManagementApplicationModule),
        typeof(AbpTenantManagementApplicationModule),
        typeof(AbpFeatureManagementApplicationModule),
        typeof(AbpSettingManagementApplicationModule),
        typeof(AbpBlobStoringModule),
        typeof(AbpBlobStoringAzureModule)
        )]
    public class MyAppApplicationModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpBlobStoringOptions>(options =>
            {
              options.Containers.ConfigureDefault(container =>
              {
                container.UseAzure(azure =>
                {
                  azure.ConnectionString = "DefaultEndpointsProtocol=https;AccountName=MyApplocalsa;AccountKey=<truncated-account-key>;EndpointSuffix=core.windows.net";
                  azure.ContainerName = "Pictures";
                  azure.CreateContainerIfNotExists = true;
                });
              });
            });
        }
    }
}

Solution

I finally figured out the answer to the question by looking at other modules in the solution.

Here is the updated code

namespace MyApp
{
    [DependsOn(
        typeof(MyAppDomainModule),
        typeof(AbpAccountApplicationModule),
        typeof(MyAppApplicationContractsModule),
        typeof(AbpIdentityApplicationModule),
        typeof(AbpPermissionManagementApplicationModule),
        typeof(AbpTenantManagementApplicationModule),
        typeof(AbpFeatureManagementApplicationModule),
        typeof(AbpSettingManagementApplicationModule),
        typeof(AbpBlobStoringModule),
        typeof(AbpBlobStoringAzureModule)
        )]
    public class MyAppApplicationModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();

            Configure<AbpAutoMapperOptions>(options =>
            {
                options.AddMaps<MyAppApplicationModule>();
            });

            Configure<AbpBlobStoringOptions>(options =>
            {
              options.Containers.ConfigureDefault(container =>
              {
                container.UseAzure(azure =>
                {
                  azure.ConnectionString = configuration.GetSection("BlobStorage:ConnectionString").Value;
                  azure.ContainerName    = configuration.GetSection("BlobStorage:ContainerName").Value;
                  azure.CreateContainerIfNotExists = true;
                });
              });
            });
        }
    }
}

I needed to add the using

using Microsoft.Extensions.DependencyInjection;

I was able to get a reference to the configuration

var configuration = context.Services.GetConfiguration();

I updated the hard coded connection string with retrieving it from the configuration.

azure.ConnectionString = configuration.GetSection("BlobStorage:ConnectionString").Value;
azure.ContainerName    = configuration.GetSection("BlobStorage:ContainerName").Value;

I updated the appsettings.json file in my Blazor app

"BlobStorage": {
    "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=myapplocalsa;AccountKey=<truncated>;EndpointSuffix=core.windows.net",
    "ContainerName" :  "Pictures"
}

That was it!

Thank you Joe for investing your time in providing answers to my question!



Answered By - Joe
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, July 30, 2022

[FIXED] how to Upload a files from Desktop to Azure blob storage using azure Function app

 July 30, 2022     azure, azure-blob-storage, azure-functions, file-upload, python     No comments   

Issue

I'm trying to Upload some files form my PC through a azure function app. At first i read some excel files then i do some Cleaning processes using Pandas and i want to upload it to Azure function app. offline every works fine, but as i published to Azure it gives Error 500. I'm guessing it can't get files from my PC.

my code looks like this:

#I get my files offline
AISauftragID = req.params.get('ID','756382')
Position = req.params.get('Position','7')
location = fr'C:\Users\bb\ff\Doc\VS Code\{ID}-{Position}'
#Some Cleaning using pandas
#connecting to Azure
blob_service_client = BlobServiceClient.from_connection_string("blablabla")
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client('foldername')
try:
    # Create new Container in the service
    container_client.create_container()
    properties = container_client.get_container_properties()
except ResourceExistsError:
    print("Container already exists.")
blob = BlobClient.from_connection_string(conn_str="blablabla",
                                                container_name="foldername",
                                                blob_name="filename") 
if blob.exists():
        logging.info('filename.csv exists')  
        return func.HttpResponse(
            "filename.csv exists",
            status_code=200)
        
else:
    blob_client = container_client.get_blob_client("filename.csv")
    # upload data
    blob_client.upload_blob(dataframe.to_csv(index=False, encoding = "utf-8"), blob_type="BlockBlob")
    return func.HttpResponse(
            "Konto.csv exists",
            status_code=200)

my function.Json file looks like this:

    {
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
   
  ]
}

Solution

I'm guessing it can't get files from my PC.

That is correct. The azure function runs somewhere in an Azure Cloud data center. There is no way this function can access the local harddrive of an on-premises workstation. Also, you really don't want to because it would be a major security risk.

You seem to have the idea to use an azure function for uploading the local file, as stated, that is a no go. You need some client process like a browser or application to send the file to azure and then you can write an azure function or whatever to process that file.

To process a file you either create an http triggered azure function that accepts a POST action with the file content or you upload the file to, for example, azure blob storage and create a blob triggered / event grid triggered azure function to process the file.



Answered By - Peter Bons
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, May 13, 2022

[FIXED] How to upload AppendBlob/a file larger than the 4mb limit into Azure Storage/Blob in Java?

 May 13, 2022     append, azure, azure-blob-storage, azure-storage, java     No comments   

Issue

How to upload a large(>4mb) file as an AppendBlob using Azure Storage Blob client library for Java?

I've successfully implemented the BlockBlob uploading with large files and it seems that the library internally handles the 4mb(?) limitation for single request and chunks the file into multiple requests.

Yet it seems that the library is not capable of doing the same for AppendBlob, so how can this chunking be done manually? Basically I think this requires to chunk an InputStream into smaller batches...

Using Azure Java SDK 12.14.1


Solution

Inspired by below answer in SO (related on doing this in C#): c-sharp-azure-appendblob-appendblock-adding-a-file-larger-than-the-4mb-limit

... I ended up doing it like this in Java:

    AppendBlobRequestConditions appendBlobRequestConditions = new AppendBlobRequestConditions()
            .setLeaseId("myLeaseId");
    
    try (InputStream input = new BufferedInputStream(
            new FileInputStream(file));) {
        byte[] buf = new byte[AppendBlobClient.MAX_APPEND_BLOCK_BYTES];
        int bytesRead;
        while ((bytesRead = input.read(buf)) > 0) {
            if (bytesRead != buf.length) {
                byte[] smallerData = new byte[bytesRead];
                smallerData = Arrays.copyOf(buf, bytesRead);
                buf = smallerData;
            }
            try (InputStream byteStream = new ByteArrayInputStream(buf);) {
                appendBlobClient
                        .appendBlockWithResponse(byteStream, bytesRead,
                                null, appendBlobRequestConditions, null,
                                null);
            }
        }
    }

Of course you need to do bunch of stuff before this, like make sure the AppendBlob exists, and if not then create it before trying to append any data.



Answered By - Jokkeri
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing