Un-Delete AWS S3 file by javascript - amazon-web-services

I have to implement the restore functionality of deleted marked files in S3. Is there any way to undo delete or remove delete marker through javascript or REST api.

First of all, the object needs to be versioned.
To undelete the object, you need to delete the delete marker. You can do this via HTTP DELETE or using the SDK's DeleteObject (here's the JavaScript SDK equivalent) and supplying the version ID of the delete marker.

Related

Completely delete the object in Amazon S3 after a certain period of time after the delete marker is assigned to it

I have versioning enabled the S3 bucket.
I want to completely delete an object after a certain period of time after the object has been given a delete marker.
Is it possible to do this using lifecycle rules?
You can probably achieve this by using two S3 Lifecycle rules:
Permanently delete previous versions of objects, for which you can specify "Number of days after objects become previous versions". I presume that deleting an object, which places a Delete Marker on the object, effectively makes the 'current' version (prior to Deletion) become a 'previous' version.
Delete expired delete markers or incomplete multipart uploads, which should then deleting the 'solo' Delete Marker.
I haven't tried that combination, so I recommend some testing.

AWS S3 Storage's Object Lock settings not working

I have created an AWS S3 bucket with Object Lock settings for Compliance Mode. While I upload a file in the bucket (And the in the File settings I can see that the Object Lock is enabled in compliance mode), I was able to delete the file. I am not sure, as per the AWS documentation, even the root user cannot delete the file with Compliance Mode Object Lock.
Please help if I am misunderstood.
Important
Object locks apply to individual object versions only.
https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lock-overview.html
Take a look at How Do I See the Versions of an S3 Object? and switch your console view to "show" object versions. You should find that you didn't actually delete the locked object version.
What you did when you "deleted" the object was create a delete marker.
A delete marker is a placeholder (marker) for a versioned object that was named in a simple DELETE request. Because the object was in a versioning-enabled bucket, the object was not deleted. The delete marker, however, makes Amazon S3 behave as if it had been deleted.
https://docs.aws.amazon.com/AmazonS3/latest/dev/DeleteMarker.html
With the console in the "hide" versions mode, delete requests are "simple DELETE requests" as mentioned above.
With the console in the "show" versions mode, delete operations you attempt are, instead, on specific versions of the object, and you should find that you are unable to delete any versions with object locks.
You'll also find that you can apparently overwrite an object with a new upload, but again you can't actually do that, because uploading an object with the same key in a versioned bucket (and enabling versioning is mandatory for object lock to work) doesn't overwrite the object -- it just creates a newer version of the object, leaving older versions intact.
When the top (newest, current) version of an object is a delete marker, the object disappears from the console and isn't included in ListObjects requests sent to the bucket via the API, but does appear in ListObjectVersions API requests. The "show/hide" setting is only applicable to your personal console view, it doesn't change actual bucket behavior.
The timestamps on object versions can't be altered, so locking an object version not only prevents deletion of the object contents, it also preserves a record of when that object was originally created. "Overwriting" an object creates a new version with a new timestamp, and the timestamps on the versions prove what content existed in the bucket at any given point in time.

Deleting object from AWS bucket

As the official document says when we enable versioning on bucket, deleted objects can be restored as only delete marker will be created.
When objects will be actually deleted then?
As if deleting them only created delete marker, they will still consume place in bucket, how many days they will be present in bucket after deleting?
If Versioning is enabled on a bucket, objects will not be deleted.
Deleting the object will merely add a delete marker
All versions of an object will be charged for storage space
However, you can delete a specific version of the object
If all versions of a document and the delete marker are deleted, then the object is fully deleted
If versioning is not enabled, then deleting an object will immediately delete it.
You can create lifecycle rules that can delete objects, or object versions, after a defined period of time.
It will be there forever, unless you manually delete all the versions and the deletion marker. You can manually delete it by going tot he bucket, on versions, click "Show", then all the versions and deletion marker will be shown. You can delete them afterwards.

S3 move files year/month wise

I have a bucket (s3://Bucket1) and there are millions of files in that with format like below:
s3://Bucket1/yyyy-mm-dd/
I want to move these files like
s3://Bucket1/year/mm
Any help, script, method will be really helpful.
I have tried aws s3 cp s3://Bucket1/ s3://Bucket1/ --include "2017-01-01*" but this is not working good and plus I have to put extra stuff to delete files.
The basic steps are:
Get a list of objects
Copy the objects to the new name
Delete the old objects
Get a list of objects
Given that you have millions of files, the best way to start is to use Amazon S3 Inventory to obtain a CSV file of all the objects.
Copy the objects to the new name
Then, write a script that reads the CSV file and issues a copy() command to copy the file to the new location. This could be written in any language that has an AWS SDK (eg Python).
Delete the old objects
Rather than individually deleting the objects, use S3 object lifecycle management to delete the old files. The benefits of using this method are:
There is no charge for the delete (whereas issuing millions of delete commands would involve a charge)
It can be done after the files have been copied, providing a chance to verify that all the files have been correctly copied (by checking the next S3 inventory output)
You could use the AWS CLI to issue a aws s3 mv command, which will combine the copy and delete -- effectively providing a rename function. However, shell scripts aren't that easy and if things fail half-way the files will be in a mixed state. That's why I prefer the "copy all objects, and only then delete" method more.

Inconsistent behavior in Amazon S3 for folder/key upload, delete through console

There seems to be some inconsistency in Amazon S3's behavior.
If in bucket a "Bucket1", I create folder "Folder1" and upload a file say "sample.txt" into it. Next I delete this file. At the bucket level I can see "Folder1" on S3 Console.
Now in the same bucket if I upload a file "Folder2/sample.txt" and just delete sample.txt file, then Folder2 also disappears from console?
Why this inconsistency? AFAIK we do not have any API to create/delete folder at SDK level.
Am I missing something here or is this an actual issue?
Thanks in advance for any help.
A "Folder" in S3 is simply a 0-byte object with a / character at the end of the key name.
So, using the AWS CLI or SDKs, you can "create a folder" by "putting" an object that matches those criteria.
The AWS Management Console also does something extra: it simulates folders, even of they were not explicitly created. So, if you uploaded your object as "Folder2/sample.txt", it extrapolates and simulates "Folder2/" at the parent folder level. You can do this yourself with the CLI/SDKs using the delimiter parameter.
When you delete that object, since "Folder2" did not actually exist as a 0-byte object ending with / (see first paragraph), then "Folder2/" disappears from the management console.