Dropbox api upload to root directory? - dropbox

I'm using dropbox-php-sdk and i cannot upload to root directory(team), i can only upload to account directory? Instead of uploading to home/Movies, it is uploading to home/Dave Smith/home/Movies?
//Configure Dropbox service
$dropbox = new Dropbox($app);
//Chunk Size
$chunkSize = 8000000;
$pathToLocalFile = '/home/mywebsite/public_html/assets/videos/input3.mp4';
// Automatically create stream through file path
$dropboxFile = new DropboxFile($pathToLocalFile);
$file = $dropbox->uploadChunked($dropboxFile, "/home/Movies/input3.mp4", null, $chunkSize);
My dropbox account has full dropbox permissions to access all files/folders in teamspace. The app/access token was created by team admin account. How do i solve?

By default, Dropbox API calls operate in the "member folder" ("/Dave Smith" in your example).
You can configure Dropbox API calls to operate in the "team space" instead though, by setting the Dropbox-API-Path-Root header. You can find information on using that in the Team Files Guide.

Related

Google Drive API : Can't delete file

I want to use Google Drive API to manage files programaticaly.
To do that, I use ID clients OAuth 2.0 Web Application. In OAuth consent screen (Edit App), I check .../auth/drive and .../auth/drive.readonly scopes.
I'm log in, I have token.json file. I can list and download files
Now, when I want to delete file, I do this in python : service.files().delete(fileId=item['id']).execute(). But I have this issue :
An error occurred: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/<fileID>? returned "The user has not granted the app <appID> write access to the file <fileID>.". Details: "[{'message': 'The user has not granted the app <appID> write access to the file <fileID>.', 'domain': 'global', 'reason': 'appNotAuthorizedToFile', 'location': 'Authorization', 'locationType': 'header'}]">
What I do wrong ?
Thanks in advance !
The scopes declared in Google cloud console are mainly for use with verification of your application. Its your code that defines what scopes your application is requesting of the user.
I am going to assume that you are following the Quick start for python or quick start for node.js both use token.json for credential storage. The answer is the same either way.
This sample shows you how to use the files.list method which allows for a readonly scope.
While the file.delete does not allow for a readonly scope it requires write access.
Fix
In the code you can see that the scope is readonly
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
This means that when the user authorized your app they authorized it with a readonly scope giving you access to read the files only. This the users credentials are now stored in token.json
To fix your issue change the scope to
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
Then delete the token.json file and your app should now be resting full drive access giving you the ability to make changes like delete the files.

Upload file to Microsoft OneDrive using graph SDK using asp.net core

I am trying to upload a file to OneDrive using Graph SDK for .Net Core from worker service.
Basically, some files are created at random time and those files needs to be uploaded to specified path on OneDrive from worker service at midnight every day.
I have following information stored in appconfig.json file in application:
ClientID
ClientSecret
TenantID
I have checked samples on various sites but could not find how to upload files using above ID and Secret. I believe there must but some kind of authProvider that I could initialize using above ID and Secret.
I also checked miscrosoft's documentation but coudl not find any example on how to upload file using SDK with ID and Secret.
https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content?view=odsp-graph-online
Additional Point
Upload new file
Overwrite if file already exists(hope that graph already supports this)
file size is < 4MB
path format /folder1/folder2/filename.pdf
Any help would be appreciated.
Remember to assign Application permission (Client credentials flow) to the app registration. See Application permission to Microsoft Graph.
You can use Client credentials provider.
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantID)
.WithClientSecret(clientSecret)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
Upload small file (<4M):
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
using var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes("The contents of the file goes here."));
await graphClient.Users[{upn or userID}].Drive.Items["{item-id}"].Content
.Request()
.PutAsync<DriveItem>(stream);
If you want to upload large file, see Upload large files with an upload session.
UPDATE:
Use Install-Package Microsoft.Graph.Auth -IncludePrerelease in Tools -> NuGet Package Manager -> Package Manager Console.
It supports both uploading a new file and replacing an existing item.
For how to get the items id, please refer to Get a file or folder.
For example, get the item id of /folder1/folder2 (have a quick test in Microsoft Graph Explorer):
GET https://graph.microsoft.com/v1.0/users/{userID}/drive/root:/folder1:/children
It will list all the children in folder1, including folder2. Then you can find item id of folder2.
UPDATE 2:
Client credentials provider is application identity while all the providers below are user identity.
Since you want to access personal OneDrive (including user identity), you could choose Authorization code provider or Interactive provider.
Authorization code provider: you need to implement interactively sign-in for your web app and use this provider.
Interactive provider: you can easily use this provider to implement interactively sign-in in a console app.
You can have a quick test with the second provider.
Please note that you should add the Delegated (personal Microsoft account) permissions into the app registration. See Delegated permission to Microsoft Graph.
And in this case, you should modify all graphClient.Users[{upn or userID}] to graphClient.Me in your code.
I'm afraid that you have to implement sign-in interactively auth flow to access personal OneDrive.

Send CSV file via Rest API

I'm not a developer, but recently have been doing lots of automations at work using VBA, Python and AWS. Recently I started learning and working with API's in AWS.
I've been trying to upload a CSV file through a REST API, but it's just not working as expected.
Basically I have used API Gateway to set up my API that triggers a Lambda Function to upload the file to a S3 bucket.
When I tested my API in Postman, it worked fine, I just had to set Headers as Key: "Content-Type" and Value: "application/csv", then in the Body part I selected "Binary" and browsed my csv file that I want to upload.
But the problem is when I tried to call my api from my application (or from Postman using the raw text instead of binary), I am sending the following Body (Also tried without double quotes and didn't work either):
--data-binary "\file-location\file-name.csv"
The api returns a success code, but when I open the file in S3, then content is "--data-binary "\file-location\file-name.csv"" instead of the actual file's content that I selected.
This is the code I'm using in my VBa to call the API:
Dim ws As Worksheet
Dim URL, env, msg, result As String
Dim objHTTP As Object
Set ws = ThisWorkbook.Worksheets("Sheet1")
URL = "https://api-url"
msg = "--data-binary ""\\file_location\file_name.csv"""
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-Type", "application/csv"
objHTTP.send (msg)
result = objHTTP.responseText
'getResponse
ws.Range("s3_api_resp").Value = result
Set objHTTP = Nothing
Uploading a file should be done directly to Amazon S3, the reason I say this, is that AWS Lambda has a limit of 6MBs as an invocation payload.
There are several ways to upload to S3, here are three ways:
SDK
CLI
Rest API
There is no SDK for VB, but if you are using VB .Net you could use the .Net SDK. An example is available here. Alternatively you could invoke the AWS CLI via VB, that is not so slick (more of a hack) but possible. Details are available here. Alternatively you can upload a file using the AWS S3 Rest APIs, details available here.
Files can be uploaded to Amazon S3 without credentials, provided the bucket policy allows open buckets. Examples of bucket policies are available here.
If the bucket is locked down (recommended) then you should use temporary credentials to upload to Amazon S3. You can get temporary credentials by either providing pre-signed URLs for PUT or you can grant temporary credentials using a service such as Amazon Cognito. Amazon Cognito comprises of two parts, User Pools to store credentials and Federated/Identity Pools. You can use Amazon Cognito Identity Pools to generate temporary credentials to Amazon Identity and Access Management using authenticated credentials. Authenticated credentials can be sourced from Apple, Google, Amazon, Cognito, SAML, and OpenID. The authenticated credentials are exchanged for IAM credentials. The IAM credentials are attached to a policy, and that policy should have permissions to upload files.

Differentiating Application Upload and FTP Client uploads

Assume we have an web application where user will be provided an option to upload folder/file
Assume we have created an IAM and configured FTP to access S3 via FTP.
Now the user can upload a folder/file via FTP or web application.
We need to create a event notification or call a rest api whenever a new folder or file has been uploaded.
How to register an event register and call the rest API whenever a new folder or file has been uploaded?
How to differentiate whether the upload or file has been done from web application or FTP server from S3 ?
We need to call a different api when the folder has been created via web application and we need to call a different api when the folder has been created via FTP server
Thanks.
1) You can register to an object created event using lambda function via the properties of the bucket(ObjectCreated(All) event), and then execute a call to your API.
2) You can add a metadata to you file while uploading it saving the source of the upload. For example in your http request add the following header x-amz-meta-filesource. Every header starting with x-amz-meta- will be saved as the file metadata.

Microsoft Graph API: How to copy Shared Folder between Accounts in OneDrive Business

Is it possible to copy a shared folder to my own profile in OneDrive Business via Microsoft Graph API?
Both accounts are OneDrive Business Accounts. Account A shares a folder to Account B. Account B is logged in and can see all shared items from Account A. So Far so good.
How can I copy this shared folder to the profile of Account B?
When I try this POST I get a 401 - The caller is not authenticated.
POST https://graph.microsoft.com/v1.0/drives/<remoteItem.driveId>/items/<id>/copy
Content-Type: application/json
Authorization: Bearer xyz...
{
"parentReference": {
"path": "/drive/root:/Documents"
}
}
The scope I use is:
mail.read
user.read
files.read
files.read.all
sites.read.all
files.readwrite
Any suggestions?
The copy API in OneDrive for Business and SharePoint is currently limited to copying files within the same "site". For OneDrive for Business each user is represented by a separate site, which means copying files between OneDrive's isn't supported.
We're working on adding the ability to copy files between sites and hope to have an update to the API available soon.