Skip to content

Instantly share code, notes, and snippets.

@klcodanr
Created November 4, 2022 16:35
Show Gist options
  • Select an option

  • Save klcodanr/933c757e758a707a2280a0d1d05e0c68 to your computer and use it in GitHub Desktop.

Select an option

Save klcodanr/933c757e758a707a2280a0d1d05e0c68 to your computer and use it in GitHub Desktop.

Revisions

  1. klcodanr created this gist Nov 4, 2022.
    35 changes: 35 additions & 0 deletions aem-assets-cs-import-example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    const fs = require("fs");
    const Path = require("path");
    const winston = require("winston");
    const {
    FileSystemUploadOptions,
    FileSystemUpload,
    } = require("@adobe/aem-upload");

    const log = winston.createLogger({
    format: winston.format.simple(),
    transports: [new winston.transports.Console()],
    });
    const args = process.argv.slice(2);
    if (args.length !== 3) {
    throw new Error(
    "Missing required arguments: aem-asset-import-example <config.json> <sourcepath> <targetpath>"
    );
    }
    const { AEM_HOST, AEM_PASSWORD, AEM_USERNAME } = require(args[0]);
    const source = Path.normalize(args[1]);
    const target = `${AEM_HOST}${args[2]}`;

    if (!fs.existsSync(source) || !fs.lstatSync(source).isDirectory()) {
    throw new Error(`Source ${source} does not exist or is not a directory`);
    }
    log.info(`Importing assets from ${source} to ${target}`);
    const options = new FileSystemUploadOptions()
    .withUrl(target)
    .withDeepUpload(true)
    .withBasicAuth(AEM_USERNAME + ":" + AEM_PASSWORD);
    const fileUpload = new FileSystemUpload({ log });
    fileUpload
    .upload(options, [source])
    .then(() => console.log(`Upload complete!`))
    .catch((err) => console.error("Upload Failed!", err));