Useful for interacting with vendors, or simply for making logs or submitting backups, you can use Mechanic's "ftp" action to upload arbitrary content to any S/FTP host.

For developers

Options

  • "host" – required, string
  • "port" – optional, integer, defaults to 21
  • "protocol" – optional, can be "ftp" or "sftp", uses an intelligent default based on other options
  • "user" – required, string
  • "password" – optional, string
  • "private_key_pem" – optional, string, used only for SFTP
  • "uploads" – required, object containing file paths mapped to file values
  • "mode" – optional, used only for FTP (not SFTP), can be set to "ascii" 

The "uploads" option is processed by Mechanic's file generators. This means that, in addition to defining simple text-based files, you can create PDFs and zip files to be uploaded to your FTP server. Read more: Generating files

Authentication

The "user" option is always required.

When working with an FTP server, for a user that has a password (recommended!), add with the "password" option.

When working with an SFTP server, use either "password" or "private_key_pem" to authenticate your user. You can supply your private key value using something like this:

{% capture private_key_pem %}
-----BEGIN OPENSSH PRIVATE KEY-----
l0UGrDQWWbOpUsLENHwD5ya478pmRXarmDj5Wh31B54nmuq7be4ZKD5eh9nEV42JCl4mX6
...
pZ/WFoT82brhooSfJDue14C0Y=
-----END OPENSSH PRIVATE KEY-----
{% endcapture %}

{% action "ftp" %}
  {
    "host": "example.com",
    "port": 22,
    "user": "sftp_user",
    "private_key_pem": {{ private_key_pem | json }}
    "uploads": {
      "success.txt": "hooray!"
    }
  }
{% endaction %}

Example

This task compiles all SKUs with their titles and prices, and uploads it as a CSV every night.

Subscriptions:

mechanic/scheduler/daily

Script:

{% assign csv = array %}

{% assign header = "SKU,Title,Price" | split: "," %}
{% assign csv[0] = header %}

{% for product in shop.products %}
  {% for variant in product.variants %}
    {% assign title = variant.title %}
    {% if title == "Default Title" %}
      {% assign title = product.title %}
    {% endif %}

    {% assign row = array %}
    {% assign row[row.size] = variant.sku %}
    {% assign row[row.size] = title %}
    {% assign row[row.size] = variant.price %}

    {% assign csv[csv.size] = row %}
  {% endfor %}
{% endfor %}

{% capture filename %}product-export-{{ "now" | date: "%Y-%m-%d" }}.csv{% endcapture %}

{% action "ftp" %}
  {
    "host": "example.com",
    "port": 21,
    "user": "anonymous",
    "password": null,
    "uploads": {
      {{ filename | json }}: {{ csv | csv | json }}
    }
  }
{% endaction %}

Legacy "put" usage

The "uploads" option was predated by an option called "put", which looked like this:

"put": {
  "mode": "ascii",
  "filename": "example.txt",
  "data": "hello world!"
}

This usage is still supported, but is not suggested. :) A modern usage would look like this:

"mode": "ascii",
"uploads": {
  "example.txt": "hello world!"
}
Did this answer your question?