AWS Boto3 S3: I accidentally renamed a set of files as empty string. They're gone right? - python-3.x

To "rename" some files I copied them with a new name and then deleted the originals. In creating the new name I meant to do this:
new_key_path = '.'.join(key_path.split('.')[0:3])
But I did this
new_key_path = '.'.join(str.split('.')[0:3])
key_path vs str. The former is a valid variable (path to file), the latter was apparently not None, but an empty string. So it didn't error out. The result of this was that all iterations set new_key_path to .. The rest of the logic was such that I was essentially copying to the "root" of the s3 bucket...
Anyway, I can get the data back elsewhere but just want to validate that I indeed messed up in this specific way. I don't see it anywhere else in the bucket. Thanks
EDIT: adding the example renaming code. This is out of the box with Boto3.
self.s3_resource.Object(self._bucket_name, new_key_path).copy_from(CopySource=copy_src)
self.s3_resource.Object(self._bucket_name, key_path).delete()

Related

Matlab writematrix in Excel gives error: Name cannot be the same as built-in name

I want to copy an Excel file to a different path through Matlab and then write in it also using Matlab. Somehow I get the error: Name cannot be the same as built-in name.
As I want to write multiple times in the file, I don't want to solve this problem manuelly each time, I want the code to run through without me having to do something constantly.
Is there any way I can solve this problem all at once through code? Does this happen because I copy the Excel file first?
The code looks like this:
path_source_template1 = 'Blabla1\Template1.xlsx';
timestamp = datestr(now);
timestamp = strrep(timestamp, ':', '-');
timestamp = strrep(timestamp, ' ', '-');
path_output = fullfile('Blabla2\',timestamp);
mkdir(fullfile(path_output));
path_output_template1 = strcat(path_output,'\Template1.xlsx');
copyfile(path_source_template1,path_output_template1);
Then I want to write in the Template1.xlsx:
writematrix(test,path_output_template1,'Sheet','Test','Range','A1',UseExcel=true,AutoFitWidth=false);
Then I get this error:
enter image description here
The input to the writematrix file uses the name, value format, so in this line:
writematrix(test,path_output_template1,'Sheet','Test','Range','A1',UseExcel=true,AutoFitWidth=false);
you should have:
..., 'Sheet','Test','Range','A1','UseExcel', true,'AutoFitWidth', false);
Disclaimer: I haven't tested this, but I'm fairly certain this will fix your problem.
The correct call to the writematrix would be:
writematrix(test,path_output_template1, 'Sheet','Test','Range','A1', 'UseExcel', true,'AutoFitWidth',false);
When writing datetime data to a spreadsheet file, you must set both 'PreserveFormat' and the 'UseExcel' Name-Value pair to true to preserve the existing cell formatting. You can check the documentation writematrix.
In order to answer to the error I tested the code locally in Matlab 2019b and works well setting the test variable in this example way: test = magic(5);.
Maybe the error could be in the data that you use in test variable or in that if you run the code iteratively very fast the path_output could exist, One way to improve this could be with a more accurate timestamp.

Amazon S3 - How to recursively rename files?

I'm trying to fetch my files via the s3.getObject() method in my node.js backend.
Trouble is, upon uploading the files to my bucket, I failed to replace special characters, dashes, and white-spaces. So, any files that have a Key value of (e.g., a Key with the value of 10th Anniversary Party (Part 1) 1-23-04 has an endpoint of 10th+Anniversary+Party+(Part+1)+1-23-04).
This becomes troublesome when trying to encode the URI for fetching. I'd like to replace all dashes, white-space, and special chars with a simple underscore. I've seen some possible conventions using the aws-cli, however I am unsure what the best command for this is. Any advice would be greatly appreciated.
You could write a program that:
Lists the contents of the bucket
Calls CopyObject() to copy the object to a new Key
Calls DeleteObject() to delete the previous copy
Or, you could take advantage of the fact that the AWS CLI offers a aws s3 mv command that will Copy + Delete for you.
I often simply create an Excel spreadsheet with the existing names, and a formula for determining what name I'd like. Then, I create a third column with:
aws s3 mv [Column 1] [Column 2]
Use Copy Down on the rows to get all the mv commands. Then, copy the column of commands, paste them into the command-line and it will rename all the objects in Amazon S3! (Test with 1-2 lines first, in case there is an error in the formula.)
This might seem primitive, but it's a very quick way to make the changes.

P4python needs to check what files are changed in a specific changelist

Using python and p4python I'm trying to shows the files that are changed in a changelist. I
result = p4.run_describe("2631893", tagged = 0)
This shows the files in a change list and not what is
result = p4.run_diff("-sa")
shows all the changed files in the client. What I am looking for is a run_diff similar functions that gives the name of changed files in a specific changelist. Is it possible?
UPDATE:
After thinking twice, I came to the fact that I shiuld probably write what I am trying to do
The idea is this that I check out some simulink models , run code generation for all models. There are already some generated code in the depot that belongs to each Simulink model. I need to check if the models generate the same code that is already in depo. If they are not the same then the name of those files should be printed. So my strategy is this
1) Make a changelist. DONE
2) check iut the models in that changelist DONE
3) check out all the already gererated files in a different change list (lets call it CL 2) DONE
4) generate code DONE
5) Revert unchanged files from that changelist (dont know how to do. It should only revert unchanged files from THAT Changelist e g. CL2)
6) if CL2 is empty then fine. Otherwise print the file name.
P4.revert('-a' , CL2)
Does not work. And i dont inow how to get the number of file in a CL from python.

Unable to copy file from SFTP in Azure Data Factory when using wildcard(*) in the filename

I am unable to copy csv files from an SFTP connection to blob storage when using the wildcard(*) in the filename.
More specifically, I receive csv files in the SFTP on a daily basis, and they are of the format: "ddMMyyyyxxxxxx.csv", where "xxxxxx" is the timestamp. More concretely, my csv file for the 13th of March is: "13032019083647.csv", while for the 14th of March: "14032019083556.csv". Obviously, the timestamp is different for every day, thus I want to copy the file independently of whatever strings exists between the date and the the file extenstion.
In the "File" subfield of the "File path" of the "Connection" tab of my subset, I give as input: "13032019*.csv", as instructed by the help icon next to the field:
When I do so, my Debug run fails with:
{"errorCode": "2200", "message":
"ErrorCode=UserErrorInvalidCopyBehaviorBlobNameNotAllowedWithPreserveOrFlattenHierarchy,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=Cannot
adopt copy behavior PreserveHierarchy when copying from folder to a
single file.,Source=Microsoft.DataTransfer.ClientLibrary}
I receive a similar error no matter which type of copy behaviour I choose. I have also tried experimenting with the fileFilter parameter (even though ADF warns that the same behaviour can be achieved with the fileName option), but I still end up getting the same error.
For further clarification, I am attaching the Code segment that ADF produces for this configuration:
I should also mention, that when using the full fileName in the corresponding field, namely the value: "13032019083647.csv", copying works normally.
Any help would be greatly appreciated!
My guess it might get two files with wildcard operation.
In such cases we need to use metadata activity, filter activity and for-each activity to copy these files.
1.Metadata activity : Use data-set in these activity to point the particular location of the files and pass the child Items as the parameter.
2.Filter activity : Use filter to filter the files based on your needs.
3.For-each activity : In the For-each activity get Items from the previous activity and add copy activity inside the for-each.
In copy activity the source data set should be #item().name.
I hope this will solve your issue.
What worked for me was the following: I kept the same regex for the input file, but I defined as "Copy behaviour: Merge Files". Since as mentioned, there is only 1 file that satisfies the regex condition, only 1 file was created as output. I am aware that this is a sort of "dirty" solution, but it did the trick for me.

svnkit: Problem getting entry name

I'm trying to create a SVN Eclipese EFS plugin and have problems when getting the names of entries.
When I make a call to:
SVNRepository
`//Fetches the contents of a directory into the provided collection object and returns the directory entry itself.
SVNDirEntry getDir(String path, long revision, boolean includeCommitMessages, Collection entries)`
It correctly returns the entry for the provided path, however, it doesn't set the "name" value on the "returned" entry. Note, the items returned in the collection are all OK.
Does anyone know why this is? And/or if there is a workaround?
See:
http://svnkit.com/javadoc/org/tmatesoft/svn/core/io/SVNRepository.html
http://svnkit.com/javadoc/org/tmatesoft/svn/core/io/SVNRepository.html#getDir(java.lang.String, long, boolean, java.util.Collection)
It correctly returns the entry for the provided path, however, it
doesn't set the "name" value on the "returned" entry. Note, the items
returned in the collection are all OK.
Treat it like "ls" command to list entries in the directory. The directory itself will be represented as ".". Or better to say that name of the entry is relative to the directory and for the directory itself it is an empty string.
Does anyone know why this is? And/or if there is a workaround?
Use dirEntry.getURL() and then you may compute name from the URL's path. Also, directory name is either part of the "path" parameter or part of the SVNRepository object location URL.
I've checked and the SVNDirEntry returned by the getDir() method always has its name attribute set to an empty string.
However when that method is used to fetch a directory listing each directory entry has its name properly assigned.
I think that behavior is somewhat buggy though you can still find out the name of an SVNDirEntry through its URL:
SVNDirEntry dirEntry = repository.getDir("branches/1.0", -1, false, null);
String name = SVNPathUtil.tail(dirEntry.getURL().getPath());

Resources