Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 copy the directory to another directory

Tags:

How do i copy/duplicate a folder that contain sub-folders and files into another directory within the S3 bucket using PHP API?

$s3->copy_object only copies the folder, but not the files and sub-folders inside.

Do i have to use $s3->list_objects to get all files and directory and run $s3->copy_object on every single file/directory?

like image 403
Clinton Avatar asked Sep 20 '12 18:09

Clinton


People also ask

How do I copy a directory in Linux to AWS s3 bucket?

To copy a local folder to a specific folder in an S3 bucket, run the s3 sync command, passing in the source directory and the full bucket path, including the directory name. The following command copies the contents of the current folder to a my-folder directory in the S3 bucket. Copied! The output shows that example.

What is the command to copy files recursively in a folder to an s3 bucket?

When passed with the parameter --recursive the aws s3 cp command recursively copies all objects from source to destination. It can be used to download and upload large set of files from and to S3.

How do I download an entire directory to AWS s3?

How to Download a Folder from AWS S3 # Use the s3 cp command with the --recursive parameter to download an S3 folder to your local file system. The s3 cp command takes the S3 source folder and the destination directory as inputs and downloads the folder.


2 Answers

S3 is not a filesystem, it's an object store. Folders don't actually exist in any tangible sense; a folder is just something you can call a shared prefix. Put another way, if you create path/to/one and path/to/two, it doesn't also cause path and path/to to exist. If you see them, that's because some component took a list of objects, split their keys on /, and decided to display that list as a hierarchy.

You want to "duplicate a folder into another folder". Rephrasing this into S3 terms, you want to "duplicate all objects with the same prefix into objects with a different prefix". Saying it that way makes the method clear: get a list of objects with the one prefix, then copy each of them.

like image 127
willglynn Avatar answered Sep 27 '22 20:09

willglynn


Code for scala (copying between folders in one bucket):

def copyFolders(bucketName: String, srcFolder: String, targetFolder: String): Unit = {
import scala.collection.JavaConversions._
val transferManager: TransferManager = TransferManagerBuilder.standard.build
try {

  for (file <- s3.listObjects(bucketName, s"$srcFolder/").getObjectSummaries) {
    val fileName = file.getKey.replace(s"$srcFolder/", "")
    if (!fileName.isEmpty) {
      val transferProcess: Copy = transferManager.copy(bucketName, file.getKey,
        bucketName, s"$targetFolder/$fileName")
      log.info(s"Old key = ${file.getKey}")
      log.info(s"New file Key = $targetFolder/$fileName")
      transferProcess.waitForCompletion()
    }
  }
} catch {
  case e: AmazonServiceException =>
    log.error(e.getErrorMessage, e)
    System.exit(1)
  case e: AmazonClientException =>
    log.error("Amazon client error: " + e.getMessage, e)
    System.exit(1)
  case e: InterruptedException =>
    log.error("Transfer interrupted: " + e.getMessage, e)
    System.exit(1)
}
}

Usage:

copyFolders("mybucket", "somefolder/srcfolder", "somefolder/targetfolder")
like image 42
Boris Mitioglov Avatar answered Sep 27 '22 20:09

Boris Mitioglov