Hi @Muhammad Afaq Zafar ,
After going through your comments, I understand that your goal is to upload the files and retrieve their shared links, which will be stored in two variables: file1 and file2. This automation will streamline your workflow, especially when dealing with more complex tasks. Below is the MATLAB code that accomplishes the task of uploading files to Dropbox and retrieving their shared links.
% Dropbox API Access Token accessToken = 'YOUR_ACCESS_TOKEN';
% File paths for the files to be uploaded filePath1 = 'path/to/your/first/file.txt'; filePath2 = 'path/to/your/second/file.txt';
% Upload files to Dropbox file1 = uploadToDropbox(filePath1, accessToken); file2 = uploadToDropbox(filePath2, accessToken);
% Display the shared links disp(['Shared link for file 1: ', file1]); disp(['Shared link for file 2: ', file2]);
function sharedLink = uploadToDropbox(filePath, accessToken) % Get the file name from the file path [~, name, ext] = fileparts(filePath); dropboxPath = ['/', name, ext]; % Path in Dropbox
% Read the file content fileContent = fileread(filePath);
Fore more information on fileread function, please refer to
% Create the HTTP request to upload the file url = 'https://content.dropboxapi.com/2/files/upload'; headers = { 'Authorization', ['Bearer ', accessToken]; 'Content-Type', 'application/octet-stream'; 'Dropbox-API-Arg', jsonencode(struct('path', dropboxPath, 'mode', 'add', 'autorename', true, 'mute', false)) };
% Perform the upload response = webwrite(url, fileContent, headers{:});
% Create a shared link for the uploaded file sharedLink = createSharedLink(dropboxPath, accessToken); end
function sharedLink = createSharedLink(dropboxPath, accessToken) % Create the HTTP request to create a shared link url = 'https://api.dropboxapi.com/2/sharing/. create_shared_link_with_settings'; headers = { 'Authorization', ['Bearer ', accessToken]; 'Content-Type', 'application/json' }; body = jsonencode(struct('path', dropboxPath, 'settings', struct('requested_visibility', 'public')));
For more information on jsonencode function, please refer to
https://www.mathworks.com/help/matlab/ref/jsonencode.html
% Perform the request to create a shared link response = webwrite(url, body, headers{:});
For more information on webwrite function, please refer to https://www.mathworks.com/help/matlab/ref/webwrite.html?s_tid=doc_ta
% Extract the URL from the response sharedLink = response.url; end
In the above code snippet, the accessToken variable holds your Dropbox API access token. You need to generate this token from the Dropbox App Console. The filePath1 and filePath2 variables specify the local paths of the files you wish to upload and uploadToDropbox function handles the file upload process. It constructs the necessary HTTP request to upload the file to Dropbox using the API endpoint for file uploads. The function also creates a shared link for the uploaded file by calling the createSharedLink function which sends a request to the Dropbox API to create a shared link for the uploaded file. It returns the URL of the shared link. Finally, the shared links for both files are displayed in the MATLAB command window.
Important tips
- Make sure that you replace 'YOUR_ACCESS_TOKEN' with your actual Dropbox API access token.
- The file paths must be correctly specified to point to the files you wish to upload.
- This code assumes that the files are text files. If you are uploading different file types, ensure that the content type is appropriately set.
Hope this helps.
Please let me know if you have any further questions.