onedrive-upload-dot-net 1.0.1

dotnet add package onedrive-upload-dot-net --version 1.0.1                
NuGet\Install-Package onedrive-upload-dot-net -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="onedrive-upload-dot-net" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add onedrive-upload-dot-net --version 1.0.1                
#r "nuget: onedrive-upload-dot-net, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install onedrive-upload-dot-net as a Cake Addin
#addin nuget:?package=onedrive-upload-dot-net&version=1.0.1

// Install onedrive-upload-dot-net as a Cake Tool
#tool nuget:?package=onedrive-upload-dot-net&version=1.0.1                

OneDriveUpload

build nuget Onedrive-upload-dot-net helps You To Upload a file to One Drive Using Your Tennant ID , Client Id , ClientSecret , UserName has a clean and structured Api and supports .Net6

Using Microsoft Graph and Azure Identity

It Uses Microsoft Graph and Azure Identity package to Connect to Microsoft One Drive Throuh ClientSecretCredential including all that is mentioned above.

Getting Started

To begin, developers need to set up authentication using the ClientSecretCredential class. This involves creating an instance of this class with essential parameters such as tennatId, ClientId, ClientSecret, and TokenCredential. The tennatId, ClientId, and ClientSecret are unique identifiers associated with the Azure AD or Microsoft Entra application.

var clientCredential = new ClientSecretCredential(
    tenantId,
    clientId,
    clientSecret,
    options
);

The TokenCredentialOptions class is crucial for configuring the token acquisition process. Developers can fine-tune authentication by setting parameters within this class. The whole process is facilitated by the versatile Microsoft.Azure.Identity namespace.

GraphClient for Microsoft Graph API

The cornerstone of OneDriveUpload is the GraphClient class. This class abstracts away the complexities of interacting with Microsoft Graph APIs, providing a simplified and efficient interface. Developers can use this class to perform various operations such as uploading files to OneDrive, accessing user information, and much more.

var graphClient = new GraphClient(clientCredential);

Comprehensive Authentication with Azure Identity

OneDriveUpload relies on the robust authentication capabilities provided by Azure Identity. The ClientSecretCredential is a part of Azure Identity and ensures secure and authorized access to Microsoft Graph APIs.

var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};  

Implementing

Configure and instantiate the GraphClient with the appropriate ClientSecretCredential:

var clientCredential = new ClientSecretCredential(
    tenantId,
    clientId,
    clientSecret,
    options
);

var graphClient = new GraphClient(clientCredential);

After Graphclient is created using this to upload to one drive using onedrive_upload_dot_net; namespace into your controller and intializing

OneDriveConnect connect = new OneDriveConnect
(_configuration["GraphSetting:TennantId"], 
_configuration["GraphSetting:ClientId"],
_configuration["GraphSetting:ClientSecret"], 
_configuration["GraphSetting:UserName"]);

and using OneDriveConnect class to use methord getUploadFileUrl to get uploaded file url.To your one drive by specifing filePath and UplaodFolderName to getUploadFileUrl methord.

var url = await connect.getUploadFileUrl(@"E:\\test.txt",_configuration["GraphSetting:folderName"]);

Sample Code

 public class OneDriveConnect :IOneDriveConnect
    {
     
        private readonly GraphServiceClient _GraphClinet;
        private readonly string UserName;
        public OneDriveConnect(string TennantId,string ClientId ,string ClientSecret,string Username)
        {
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };  
            this.UserName = Username;
            var credentials = new ClientSecretCredential(TennantId, ClientId, ClientSecret, options);
            _GraphClinet = new GraphServiceClient(credentials, scopes);
            
        }

        public async Task<string> getUploadFileUrl(string filePath,string UploadFolderName)
        {
            try
            {
                var filename = Path.GetFileName(filePath);
                var key = $"{UploadFolderName}/{filename}";


                /* var fileName = "test.txt";*/
                /*    var combine = Path.Combine(filePath, filename);*/
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException("This file was not found.");

                }
                FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                //Accessing Drive using Graphclient
                var drive = await _GraphClinet.Users[UserName].Drive.GetAsync();
                if (drive.Id == null)
                {
                    throw new Exception("Drive not found");
                }
                //Uploading File to One Drive 
                await _GraphClinet.Drives[drive.Id].Root.ItemWithPath(key).Content.PutAsync(file);
                //Making File Editable and For accessible to anyone that is uploaded
                Microsoft.Graph.Drives.Item.Items.Item.CreateLink.CreateLinkPostRequestBody body = new()
                {
                    Type = "edit",
                    Scope = "anonymous",

                };
                //creating shareable link of the file uploaded
                var result = await _GraphClinet.Drives[drive.Id].Root.ItemWithPath(key).CreateLink.PostAsync(body);
                return result.Link.WebUrl;
            }
            catch 
            {
                throw;
            }
        }
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 180 1/16/2024
1.0.0 121 1/12/2024