Share via

Can we copy files from Shared point site to blob storage by using Azure Function App

23 Sept 2024, 10:03 am

Hi,

I am trying to copy files from Shared point library to Blob Storage by using Azure function app if that's possible - Though I've seen one method by using logic apps.
Can any one guide me on that?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,141 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,934 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,868 questions
0 comments No comments
{count} votes

5 answers

Sort by: Oldest
  1. AzureAce 102 Reputation points
    23 Sept 2024, 10:15 am

    Hi @Zaheer. Sadaf (Enterprise Services)

    Thank you for posting this in Microsoft Q&A.

    Yes, To copy files from SharePoint to Azure Blob Storage using an Azure Function App. Here's a steps

    1. Create Azure Function App:

    Set up a Function App in Azure.

    Choose a Consumption Plan for cost-efficiency.

    2. Create a Blob Trigger Function:

    Create a function triggered by an HTTP request.

    Use a Blob output binding to write files to Blob storage.

    3. Install Required NuGet Packages:

    Install packages for SharePoint Online CSOM and Azure Storage.

    4. Write Function Logic:

    Authenticate to SharePoint.

    Retrieve files from the specified list or folder.

    Download files from SharePoint.

    Upload files to Blob storage using the output binding.

    I hope this helps! Please mark as "Accept the answer" if the above steps helps you. Your suggestion will help others!


  2. AzureAce 102 Reputation points
    24 Sept 2024, 8:11 am

    Hi @Zaheer. Sadaf (Enterprise Services)

    Please accept as "Yes" if the answer was helpful, so that it can help others in the community.

    0 comments No comments

  3. AzureAce 102 Reputation points
    8 Oct 2024, 10:08 am

    Hi @Zaheer. Sadaf (Enterprise Services)

    Yes, you need to provide permissions to the SharePoint service you want to consume using the REST API. These permissions determine what actions you can perform on the service.

    There are two main ways to provide permissions:

    1. App-only permissions:
      Use this method when you want to create an application that will interact with SharePoint on behalf of a user
      Need to register your application with Azure AD and provide it with the necessary permissions.
    2. User context permissions: Use this method when you want to perform actions on SharePoint using the credentials of a specific user.
      Need to get an access token for the user using their credentials and include it in your REST API requests. I hope this helps! Please mark as "Accept the answer" if the above steps helps you. Your suggestion will help others!

  4. Hardikbhai Velani 75 Reputation points
    24 Sept 2024, 8:13 am

    Yes Zaheer, it's possible to copy files from SharePoint library to Blob Storage using Azure Function App. Here's a step-by-step guide:

    Prerequisites:

    1. Azure Function App (v3 or later)
    2. SharePoint Online (or On-Premises with appropriate configuration)
    3. Azure Blob Storage
    4. Azure Storage Account
    5. SharePoint Client ID and Secret (for authentication)

    Method 1: Using SharePoint REST API and Azure Storage SDK

    1. Create a new Azure Function App (C# or Node.js).
    2. Install required NuGet packages (C#):
      • Microsoft.SharePoint.Client
      • Microsoft.Azure.Storage.Blob
    3. Import necessary namespaces.
    4. Authenticate to SharePoint using Client ID and Secret.
    5. Use SharePoint REST API to retrieve file contents.
    6. Use Azure Storage SDK to upload file to Blob Storage.

    C# Example:

    
    using Microsoft.SharePoint.Client;
    
    using Microsoft.Azure.Storage.Blob;
    
    [FunctionName("CopyFileToBlob")]
    
    public static async Task Run(
    
        [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
    
        ILogger logger)
    
    {
    
        // SharePoint authentication
    
        var clientContext = new ClientContext("(link unavailable)");
    
        clientContext.ExecutingWebRequest += (sender, e) => {
    
            e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + GetAccessToken();
    
        };
    
        // Retrieve file from SharePoint library
    
        var file = clientContext.Web.GetFileByUrl("(link unavailable)");
    
        clientContext.Load(file);
    
        await clientContext.ExecuteQueryAsync();
    
        // Upload file to Blob Storage
    
        var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=your-storage-account;AccountKey=your-storage-key;BlobEndpoint=(link unavailable)");
    
        var blobClient = storageAccount.CreateCloudBlobClient();
    
        var container = blobClient.GetContainerReference("your-container");
    
        var blob = container.GetBlockBlobReference("your-file.txt");
    
        await blob.UploadFromStreamAsync(file.OpenBinaryStream());
    
    }
    
    

    Method 2: Using Microsoft Graph API and Azure Storage SDK

    1. Register your Azure Function App in Azure AD.
    2. Grant necessary permissions to Microsoft Graph API.
    3. Use Microsoft Graph API to retrieve file contents.
    4. Use Azure Storage SDK to upload file to Blob Storage.

    Node.js Example:

    
    const { Client } = require('@microsoft/microsoft-graph-client');
    
    const { BlobServiceClient } = require('@azure/storage-blob');
    
    const client = new Client(clientId, clientSecret, tenantId);
    
    const blobServiceClient = new BlobServiceClient(`https://${storageAccountName}.(link unavailable)`, storageAccountKey);
    
    module.exports = async function (context, timer) {
    
        // Retrieve file from SharePoint library using Microsoft Graph API
    
        const file = await client.api('/sites/your-site/drive/items/your-file-id/$value').get();
    
        // Upload file to Blob Storage
    
        const containerClient = blobServiceClient.getContainerClient('your-container');
    
        const blockBlobClient = containerClient.getBlockBlobClient('your-file.txt');
    
        await blockBlobClient.uploadData(file.value, file.size);
    
    };
    
    

    Logic App Alternative:

    If you prefer using Logic Apps, create a new Logic App with:

    1. SharePoint trigger (e.g., "When a file is created or modified")
    2. Azure Blob Storage action (e.g., "Create blob")

    Configure the trigger and action to copy files from SharePoint library to Blob Storage.

    Let me know if you need further assistance or clarification!

    0 comments No comments

  5. 3 Oct 2024, 8:14 am

    Hi,

    To use SharePoint REST API, do I need to provide any permissions to the SharePoint service to be consumed?
    if yes, then what would be those permissions?

    Thanks,
    Sadaf Zaheer

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.