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 FTP or SFTP host.

For developers

Options

  • "host" – required, string
  • "port" – optional, integer, defaults to 21; must be set to 22 to use SFTP
  • "user" – required, string
  • "password" – optional, string
  • "put" – required object, containing the properties detailed below

Put options

  • "mode" – optional, defaults to "ascii", may be set to "binary" 
  • "filename" – required, may be either a simple filename or an entire upload path including filename
  • "data" – required, string, defining the entire file contents to upload

Example

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

Subscriptions:

mechanic/scheduler/daily

Script:

{% capture newline %}
{% endcapture %}

{% assign csv = "SKU,Title,Price" | append: newline %}

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

    {% comment %}
      Fully escape the title, to make sure that things like
      quotes and commas don't interfere with the csv structure.
    {% endcomment %}
    {% assign title = title | json %}

    {% assign csv = csv | append: variant.sku | append: "," %}
    {% assign csv = csv | append: title | append: "," %}
    {% assign csv = csv | append: variant.price | append: newline %}
  {% endfor %}
{% endfor %}

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

{
  "action": {
    "type": "ftp",
    "options": {
      "host": "example.com",
      "port": 21,
      "user": "anonymous",
      "password": null,
      "put": {
        "mode": "ascii",
        "filename": {{ filename | json }},
        "data": {{ csv | json }}
      }
    }
  }
}
Did this answer your question?