How to upload to Dropbox Public folder and get public URL? - python

I have a situation, where I need to upload a file to my Dropbox Public Folder, and also once uploaded I need to store the uploaded file's public url ? I am using python, and any help on this would be great.
Thanks.

Use this to set up a Python SDK in your program
https://www.dropbox.com/developers/start/setup#python
This will give you all of the file information:
folder_metadata = client.metadata('/')
I beleive you are talking about these short links, just so you know, every small link from the public folder is generated only by special request and has an expiration date.
If you want a permanent link skip to step 2.
STEP 1
This information was taken from: https://www.dropbox.com/developers/reference/api
/shares
DESCRIPTION
Creates and returns a shareable link to files or folders.
Note: Links created by the /shares API call expire after thirty days.
URL STRUCTURE
https://api.dropbox.com/1/shares/<root>/<path>
root The root relative to which path is specified. Valid values are sandbox and dropbox.
path The path to the file or folder you want a shareable link to.
VERSIONS
0, 1
METHOD
POST
PARAMETERS
locale Use to specify language settings for user error messages and other language
specific text. See the notes above for more information about supported locales.
RETURNS
A shareable link to the file or folder. The link can be used publicly and directs to a preview page of the file. Also returns the link's expiration date in Dropbox's usual date format.
Sample JSON return value for a file
{
"url": "http://db.tt/APqhX1",
"expires": "Wed, 17 Aug 2011 02:34:33 +0000"
}
If you did step 1 don't do step 2.
STEP 2
/files (GET)
DESCRIPTION
Downloads a file. Note that this call goes to the api-content server.
URL STRUCTURE
https://api-content.dropbox.com/1/files/<root>/<path>
root The root relative to which path is specified. Valid values are sandbox and dropbox.
path The path to the file you want to retrieve.
VERSIONS
0, 1
METHOD
GET
PARAMETER
rev The revision of the file to retrieve. This defaults to the most recent revision.
RETURNS
The specified file's contents at the requested revision.
The HTTP response contains the content metadata in JSON format within an x-dropbox-metadata header.
ERRORS
404 The file wasn't found at the specified path, or wasn't found at the specified rev.
NOTES
This method also supports HTTP Range Retrieval Requests to allow retrieving partial file contents.`
DONE

Related

SharePoint list files from under a URL

I have a simple task to do: script reads a free-text string which will contain SharePoint URLs. Now those URLs are provided by the users, basically a copy-paste from their browser. The thing that my app has to do is go to those links and check if there are any files under it.
So from what I can gather, there are many possible SharePoint URLs, for example:
<host>/sites/<site_name>/SitePages/something.aspx - for example a simple post
<host>/:w/r/sites/<site_name>/_layouts/15/something.aspx (like a shortcut URL) - for example a MS Office Word document
<host>/sites/<site_name>/<drive_name>/Forms/something.aspx?[...]&id=%2Fsites%2F<site-name>%2F<drive_name>%2F<path> - a URL to a file tree view of some files on a drive
<host>/:f:/r/sites/<site_name>/<drive_name>/<path_to_a_file>
The last one is perfect, because it contains the path to the directory in the url path. The 3rd one does have it as well, but in the urlencoded query params part.
What I do in this scenario is I parse the URL, extracting:
site name
drive name (not ID)
path (from the path in url or from the encoded &id= part)
Then, I can connect to SharePoint, get a site, list all the site drives (/drives), check if their "web_url" is a substring of my Sharepoint URL (I could search the appropriate drive by name, but the thing returned from the API is the "display name" and in my URL resides an "actual drive name"). Okay, so I've got my drive and now I can get my item by path. This all can be done via the regular MS Graph API (each step is needed for getting the object - site/drive ID) or via a python wrapper (I use python-o365).
As you can see, this is a real pain. Is there a standard way to deal with this? I mean, if I had the site and drive IDs, I could do it in a single API call, but given the fact that I only have a SharePoint link, I can't get those two, right? And how about the URL parsing?

Google Cloud Storage - Python Client - Get Link URL of a blob

The Link URL at the Object details in the GoogleCloudStorage browser follows the template:
https://storage.cloud.google.com/<project_name>/<file_name>?authuser=0&organizationId=<org_id>
I'm trying to get the exact same link using the python package google-cloud-storage. Diving into the blob properties I've found the followings (none of which are exactly what I need):
self_link: https://www.googleapis.com/storage/v1/b/<project_name>/o/<file_name>
media_link: https://storage.googleapis.com/download/storage/v1/b/<project_name>/o/<file_name>?generation=<gen_id>&alt=media
Note: If I replace storage.googleapis.com with storage.cloud.google.com at the media_link I get to download the file as I expect (getting asked for a valid Google Account with the required permissions).
Is there any way to get the link directly from the blob object?
Here the pattern:
https://storage.googleapis.com/<BUCJET_NAME>/path/to/file
For example, for a bucket my_bucket and a file stored in this path folder_save/2020-01-01/backup.zip, you have this url https://storage.googleapis.com/my_bucket/folder_save/2020-01-01/backup.zip
I think that the best approach is manually generate the URL that you need by replacing the domain of the URL.
In client library source I couldn’t find any reference to a method/property in the blob class that uses the domain “storage.cloud.google.com”
even using the public url property the result is the URL that points to googleapis

Building a web application that can work with local files

I'd like to build an application (local, not online) by using front-end web technology for the UI, the application simply displays PDFs and has a few text fields for the user to fill in with regards to the current PDF they're viewing, the user can then export their notes and a file path to the document in CSV file format.
comment about file, some more notes, C:\somefolder\doc1.pdf
comment about file, some more notes, C:\somefolder\doc2.pdf
My first issue, JavaScript can't access the local file system, so I used a file upload form which worked except the filepaths were shown as blob filepaths and not the actual system file path. Other than that my "application" worked as intended.
I went and learned Flask in hopes of using python for the back end, which works great except when I pass in the file path to the pdf C:\SomeFolder\doc1.pdf inside the 'src' attribute for an Chrome says it can't access local files. SO I'm back to sqaure one!
How can I go about building this application with local file access?
If you need to access the local files, you can create an endpoint in flask that launches a file dialog GUI. This only works because you application is hosted locally. You can use either tkinter or the native windows API using win32ui.
Assuming you are using the standard Flask format:
from app import app
#app.route('/file_select', methods=['GET', 'POST'])
def file_select():
from tkinter import Tk
from tkinter.filedialog import askopenfilename
root = Tk()
root.withdraw()
# ensure the file dialog pops to the top window
root.wm_attributes('-topmost', 1)
fname = askopenfilename(parent=root)
return jsonify({'filepath': fname})
or using the win32ui API
#app.route('/file_select', methods=['GET', 'POST'])
def file_select():
import win32ui
winobj = win32ui.CreateFileDialog(1, ".pdf", "", 0,
"PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*|")
winobj.DoModal()
return jsonify({'filepath': winobj.GetPathName()})
Now just add a button that points to the /file_select route and you will open a file dialog via the python local server and return the selected file.
Assuming you are accessing the page via http://localhost:8080/page or something like that, you should serve your content via that approach. Effectively, rather than serving the files as paths on the local file system, you would create an application route and associate it with a handler than retrieves the appropriate PDF from the local filesystem, and then sends back a response containing Content-Type: application/pdf in the HTTP response headers and the bytes of the PDF file in the response body.
To avoid duplicating someone else's solution for the approach described about, I would recommend taking a look at this answer for "Flask handling a PDF as its own page".
Because you are technically sending the response back from localhost -- or whatever name you are serving it with -- rather than trying to load a local file directly from the client's web-page, Chrome shouldn't throw any complaints.
Of course, it's worth noting that best practices should be taken when determining the file to load, if this were going to be anything more than a learning project. In any legitimate system that did this kind of thing, it would be necessary to perform checks on the requested files to ensure a malicious user does not abuse the application to leak files from the local filesystem, beyond those files which are intended to be served. (To that end, you typically might have the src element contain a parameter that is set to the hash/unique ID for the file which is then mapped via some database to the correct path of the file. Alternatively, you might use a param in the src that contains the name of the file without the full path, and then check that the user-provided value for that parameter in the request does not contain any characters outside of a charset like [a-zA-Z0-9_-].) Ultimately, it sounds like this particular warning doesn't apply to your case, but still providing it in case anyone else reads this in the future.
I think mht is exactly what you want. mht is a file extension recongnized by IE. Internally it is an HTML file. IE (only) treats a mht file with the same security restrictions that a exe might have. You could access the file system, delete a file, display a file etc.. It is everything that html/javascript security was trying to prevent. Now that IE has changed significantly I don't know what the support for this is nowadays. I couldn't find a reference page to give you a link, but it is simple enough - just save a html file with an mht extension

How to upload files to an HTTP location using a python script

I am looking for a script which will upload files from a unix server to an HTTP location.
I want to use only these modules - cookielib, urllib2, urllib, mimetypes, mimetools, traceback
I would like to make question more clear
here http location mentioned is a common share folder link which can use by number of people by accessing through username/passwd.
for ex: my target location has:
I have http:/commonfolder.mydomain.com/somelocation/345333 location
I have username = someuid ,password = xxxxx
my source location has:
I have a my.txt file contains some data
I want to run shell or python script which will tranfer the file to target location mentioned above. so that many people can start using latest info I am uploading timely.
-shijo

Check url is a file or directory

Hi there.
I have a URL list. I do not know how to check is this address to a file or directory.
examples:
url = "http://example.com/path/to/file.html"
if '.' in url.split('/')[-1]:
return True
but if url is
url = "http://example.com/path/domains/domain.com"
domain.com is a directory not a file. How to detect it?
Checking the file extension is not good, maybe some headers? But I want to do as little as possible internet transfer usage.
Edit:
I need to download a large number of links and map their path to the location in my operating system. eg
example.com/path/to/file.html
~/Downloads/example.com/path/to/
and here download file.html.
eg:
example.com/directory/
create ~/Downlods/example.com/directory/
next url: example.com/directory/dir2
create ~/Downloads/example.com/directory/dir2
next url: example.com/directory/file.html
Download file.html in too ~/Downloads/example.com/directory/
not too create file.html directory
In short, you can't. Accessing the URL http://example.com/path/domains/domain.com would send a 302 redirect (if I remember correctly) to http://example.com/path/domains/domain.com/ by default. There are no headers in the response that indicates if a URL points to a directory. May I ask why you need to know this? I suppose you can add a slash to a URL and see what happens from there. That might get you the results you are looking for.
On HTTP servers, there is no such "file" or "directory" things. You just send an URI to the server which identify a specific resource that depends of the server's configuration.
By default, most of the HTTP servers use the files and directories of your system, but it can be configured (URL Rewriting, ...).

Categories