# Upload asset folder structure

**URL:** <https://discourse.getcockpit.com/t/upload-asset-folder-structure/1336>\
**Category:** Support\
**Created:** [April 7, 2020, 8:27pm UTC](https://discourse.getcockpit.com/t/upload-asset-folder-structure/1336 "2020-04-07T20:27:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnagnu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/gnagnu/32/572_2.png) [@gnagnu](https://discourse.getcockpit.com/u/gnagnu)\
**Post date:** [April 7, 2020, 8:27pm UTC](https://discourse.getcockpit.com/t/upload-asset-folder-structure/1336/1 "2020-04-07T20:27:52Z")

</div>

Hi Cockpit team and community!

I have a library of photos organized in folders/subfolders which I would like to upload to Cockpit Assets.

Is there a way to upload a complete folder with all the files and subfolders? Otherwise, is there an API method I could use to do it programmatically ?

Thank you!

Hang

---

<div class="post-metadata">

**Author:** ![pauloamgomes](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/pauloamgomes/32/48_2.png) [@pauloamgomes](https://discourse.getcockpit.com/u/pauloamgomes)\
**Post date:** [April 10, 2020, 8:27pm UTC](https://discourse.getcockpit.com/t/upload-asset-folder-structure/1336/2 "2020-04-10T20:27:53Z")

</div>

You can do that using the REST API, or by creating a CLI command, in both cases is a straightforward process.

If building a CLI command and assuming your cockpit installation has access to the photos folder:

1. Iterate over the folder
2. Create folder with same name in Cockpit
3. Iterate of the images in the folder, create them on Cockpit using the API

to create a folder you can do something like:

```auto
$folder = [
  'name' => $folder_name,
  '_p' => $parent_folder_name // if any
];

$app->storage->save('cockpit/assets_folders', $folder);

```

keep in mind folders in Cockpit are virtual, they are tagged to the images as an attribute

for the images you need to create them on cockpit (e.g. `$app->storage->insert('cockpit/assets', $asset);` and copy to the storage, there is CLI import command in the core, maybe you can check the code there: `modules/Cockpit/cli/import/assets.php`

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [April 10, 2020, 8:54pm UTC](https://discourse.getcockpit.com/t/upload-asset-folder-structure/1336/3 "2020-04-10T20:54:45Z")

</div>

@pauloamgomes Thanks for the concept. 🙂

@gnagnu I wrote that cli command in the last hours. It is a feature, that I’m missing for a long time. I tested it only on my localhost on a Windows machine, but it seems to work well.

Usage:

```bash
$ ./cp assets-folder-import --dir "/e/cpimageimport" --list
cpimageimport
  0005_test.jpg
  ...
  test.txt

```

```bash
$ ./cp assets-folder-import --dir "/e/cpimageimport" --ext jpg,png --r --list
cpimageimport
  0005_test.jpg
  ...
cpimageimport > nature
  0017_test.jpg
  ...
cpimageimport > nature > flowers
  0037_test.jpg
  ...
cpimageimport > urban
  0001_test.jpg
  ...

```

```bash
$ ./cp assets-folder-import --dir "/e/cpimageimport" --ext jpg,png --r
Created new virtual folder nature - 5e90d8a39cb38e1b7c006d82
Created new virtual folder flowers - 5e90d8a39cb38e1b7c006d83
Created new virtual folder urban - 5e90d8a39cb38e1b7c006d84
Start to import assets. This may take a while...
Imported 1 of 16 files | mem: 4 MB | peak: 4 MB
...
Imported 16 of 16 files | mem: 30 MB | peak: 106 MB
Imported 16 assets in 52 seconds

```

The memory and the peak output is because the image import might break your memory limit (ColorThief and SimpleImage are two memory heavy libraries, that are called for each asset).

> <https://github.com/raffaelj/cockpit-scripts/blob/master/cli/assets-folder-import.php>

---

<div class="post-metadata">

**Author:** ![gnagnu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/gnagnu/32/572_2.png) [@gnagnu](https://discourse.getcockpit.com/u/gnagnu)\
**Post date:** [April 15, 2020, 7:41pm UTC](https://discourse.getcockpit.com/t/upload-asset-folder-structure/1336/4 "2020-04-15T19:41:09Z")

</div>

Thanks @pauloamgomes @raffaelj for your answers. I was able to get it done 🙂
