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

2024-09-23T10:03:20.4533333+00:00

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.
4,973 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,834 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,675 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. AzureAce 102 Reputation points
    2024-09-23T10:15:40.1766667+00:00

    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
    2024-09-24T08:11:10.15+00:00

    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. Hardikbhai Velani 75 Reputation points
    2024-09-24T08:13:53.78+00:00

    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

  4. 2024-10-03T08:14:42.3+00:00

    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

    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.