I am trying to upload some files to dropbox using their java API (version 2-beta-4), but some of these files have the same name.
What I would like to know is: What is the reason for I send a file (for instance "file.txt") to dropbox, this file is uploaded, but if I send another file with the same name (file.txt) dropbox overwrite the old file with this new one instead of renaming it to "file (1).txt", even I am setting autorename true and the WriteMode to add?
Code:
DbxRequestConfig config = new DbxRequestConfig("sample", "pt_BR");
String acessToken = "...";
client = new DbxClientV2(config, accessToken);
InputStream input = new ByteArrayInputStream(file.getBytes());
FileMetadata file = client.files.uploadBuilder(path).mode(WriteMode.add).autorename(true)
.mute(true).run(input);
Thanks.
WriteMode.add is what's causing this behavior. "Add" means "Add a new file with this name," so it never overwrites an existing file. If you want to overwrite the existing file, use WriteMode.overwrite.
(Also, isn't it WriteMode.add() and WriteMode.overwrite()? I thought those were methods.)
Related
So in google-cloud-storage if you upload more than one file with the same name to it the last will overwrite what was uploaded before it.
If I want to upload more than one file with the same name I should append some unique thing to the file name e.g. timestamp, random UUID.
But by doing so I'll lose the original file name while downloading, because I want to serve the file directly from google.
If we used the unique identifier as a folder instead of appending it to the file name, e.g. UUID +"/"+ fileName then we can download the file with its original name.
You could turn on Object Versioning which will keep the old versions of the object around.
Alternatively, you can set the Content Disposition header when uploading the object, which should preserve whatever filename you want on download.
instead of using object versioning, you can attach the UUID (or any other unique identifier) and then update the metadata of the object (specifically the content disposition), the following is a part of a python script i've used to remove the forward slashes - added by google cloud buckets when to represent directories - from multiple objects, it's based on this blog post, please keep in mind the double quotes around the content position "file name"
def update_blob_download_name(bucket_name):
""" update the download name of blobs and remove
the path.
:returns: None
:rtype: None
"""
# Storage client, not added to the code for brevity
client = initialize_google_storage_client()
bucket = client.bucket(bucket_name)
for blob in bucket.list_blobs():
if "/" in blob.name:
remove_path = blob.name[blob.name.rfind("/") + 1:] # rfind gives that last occurence of the char
ext = pathlib.Path(remove_path).suffix
remove_id = remove_path[:remove_path.rfind("_id_")]
new_name = remove_id + ext
blob.content_disposition = f'attachment; filename="{new_name}"'
blob.patch()
In the workspace we have a set of resource files which contain contents in it.
Problem: When I select the project we can get the resource file and now we want to clear the contents of the file. So we tried to get the resource file as IFile and if we try to use the IFile::delete() method it is deleting the resource file from workspace instead of clearing the contents.
How can we clear the contents of the IFile present in the workspace without deleting the IFile form the workspace?
Use IFile::setContents() to change the contents of a file.
To clear the content of the file, use an empty input stream:
InputStream newContents = new ByteArrayInputStream( new byte[ 0 ] );
IProgressmonitor monitor = ...
file.setContents( newContents, IResource.KEEP_HISTORY, monitor );
The KEEP_HISTORY flag keeps the previous contents in the history so that user could go back to earlier versions with Replace With > Local History. If that's not desired, use IResource.NONE.
'pickAndStore' method allows me to specify full path to the file, but I don't know it's extension at this point (file path has to be defined before file is uploaded, so it's not possible to provide a path with correct extension).
if I use 'pick' and then 'store' I have 2 files (because both methods uploads file to the s3). I can delete 'old' file, but it's not optimal and can be pain (take ages) with really big files.
Is there any better solution? Ideally to rename existing file.
Currently, there is no workaround for renaming file.
However, in our Javascript API v2 we are planing to add new callback function. onStart callback will be fired after user pick file but before file uploading. There could be option like renaming file based on original filename.
We will keep you updated.
I started doing development with google scripts few days ago and recently joined stackoverflow. I have a problem with addToFolder() function. I have the following piece of code that copies my new spreadsheet into a folder (test/sheets) in my Google Drive:
var ss = SpreadsheetApp.create("test");
var ssID = ss.getId();
DocsList.getFileById(ssID).addToFolder(DocsList.getFolder("test/sheets"));
My problem is that now I have 2 versions of the same file (one in the root of my Google Drive folder and the other in test/sheets folder), whenever I try to delete either of the copies, the other copy is deleted as well. Is there a way to delete the old file and keep the new one OR is there a way to create the file in the desired folder in first place?
EDIT :
thanks for you quick response. I played with this for couple of hours but still have problem copying the file to the destination folder. The problem is that even when I use makeCopy Method of the file, still addToFolder is the only option to mention the folder. Again this ends up having the tagged filename in the destination folder.
I had the same problem with the copy method.
Here is my new Code:
var SetLocationFile = "icompare/sheets/stocks"
var FolderID = DocsList.getFolder(SetLocationFile);
var FileID = DocsList.getFileById(ssID);
FileID.makeCopy("test3").addToFolder(FolderID);
Folders in Google Docs\Google Drive are actually tags. When you "add" a file to the folder "test/sheets", you do not make a copy of your file, you just attach the tag "test/sheets" to it. Now the same file is shown both in the "test/sheets" folder (i.e. in the list of all files with the tag "test/sheets") and in the root. If you wish to make a copy of the file, you should use the copy method. (Please let me know if I just misunderstand your question.)
I realize this is an old questions but you can simply use .removeFromFolder(DocsList.getRootFolder()); to remove the file from the root folder.
I would also like to know the answer to this question.. seems rather "weird" that the API does not even provide a way to create spreadsheets and place them in a certain map? And no, I do not want a Copy of the file, I want the file to be in a specific map and in no other map...
I am using wicket framework, and I have made a zip file by Java code, I want to have a link to download it, I don't know if it is possible or I should make the zip file by wicket (but not Java) and then have a link to download.
Take a look at ZipResourceStream. With this class you can generate zip contents of a directory on the fly, and use a org.apache.wicket.markup.html.link.ResourceLink with ResourceStreamResource to link to it.
File file = new File(path);
IResourceStream resStream = new ZipResourceStream(file);
ResourceStreamResource resource = new ResourceStreamResource(resStream);
ResourceLink link = new ResourceLink("link", resource);
add(link);
Alternatively, if you prefer to zip the file with another tool, you can use DownloadLink:
File zipFile = generateZipFile();
IModel fileModel = new Model(zipFile);
add(new DownloadLink("dllink", fileModel);
If you prefer to generate the File on the fly in the Link's onClick, take a look at this question: How to use Wicket's DownloadLink with a file generated on the fly?