{"id":13482921,"url":"https://github.com/AssetSync/asset_sync","last_synced_at":"2025-03-27T13:33:04.733Z","repository":{"id":37821925,"uuid":"2126953","full_name":"AssetSync/asset_sync","owner":"AssetSync","description":"Synchronises Assets between Rails and S3","archived":false,"fork":false,"pushed_at":"2024-08-29T00:45:21.000Z","size":808,"stargazers_count":1884,"open_issues_count":63,"forks_count":346,"subscribers_count":32,"default_branch":"master","last_synced_at":"2024-10-29T13:50:47.229Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AssetSync.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-07-30T00:18:02.000Z","updated_at":"2024-10-10T00:34:09.000Z","dependencies_parsed_at":"2024-06-18T12:24:50.420Z","dependency_job_id":"c0b2bde4-b85d-4643-94c6-8adf7ef1e6fe","html_url":"https://github.com/AssetSync/asset_sync","commit_stats":{"total_commits":576,"total_committers":129,"mean_commits":4.465116279069767,"dds":0.6840277777777778,"last_synced_commit":"4deb6ff0a0bb559797467e08bf0f5035cc6b71ba"},"previous_names":["rumblelabs/asset_sync"],"tags_count":82,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AssetSync%2Fasset_sync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AssetSync%2Fasset_sync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AssetSync%2Fasset_sync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AssetSync%2Fasset_sync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AssetSync","download_url":"https://codeload.github.com/AssetSync/asset_sync/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222262586,"owners_count":16957610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:06.712Z","updated_at":"2024-10-30T16:31:52.394Z","avatar_url":"https://github.com/AssetSync.png","language":"Ruby","readme":"\n[![Gem Version](https://img.shields.io/gem/v/asset_sync.svg?style=flat-square)](http://badge.fury.io/rb/asset_sync)\n[![GitHub Build Status](https://img.shields.io/github/actions/workflow/status/AssetSync/asset_sync/tests.yaml?branch=master\u0026style=flat-square)](https://github.com/AssetSync/asset_sync/actions/workflows/tests.yaml)\n[![Coverage Status](http://img.shields.io/coveralls/AssetSync/asset_sync.svg?style=flat-square)](https://coveralls.io/r/AssetSync/asset_sync)\n\n\n# Asset Sync\n\nSynchronises Assets between Rails and S3.\n\nAsset Sync is built to run with the new Rails Asset Pipeline feature introduced in **Rails 3.1**.  After you run __bundle exec rake assets:precompile__ your assets will be synchronised to your S3\nbucket, optionally deleting unused files and only uploading the files it needs to.\n\nThis was initially built and is intended to work on [Heroku](http://heroku.com) but can work on any platform.\n\n## Upgrading?\n\nUpgraded from 1.x? Read `UPGRADING.md`\n\n## Installation\n\nSince 2.x, Asset Sync depends on gem `fog-core` instead of `fog`.  \nThis is due to `fog` is including many unused storage provider gems as its dependencies.  \n\nAsset Sync has no idea about what provider will be used,  \nso you are responsible for bundling the right gem for the provider to be used.  \n\nIn your Gemfile:\n```ruby\ngem \"asset_sync\"\ngem \"fog-aws\"\n```\n\nOr, to use Azure Blob storage, configure as this.\n\n``` ruby\ngem \"asset_sync\"\ngem \"gitlab-fog-azure-rm\"\n\n# This gem seems unmaintianed\n# gem \"fog-azure-rm\"\n```\n\nTo use Backblaze B2, insert these.\n\n``` ruby\ngem \"asset_sync\"\ngem \"fog-backblaze\"\n```\n\n\n### Extended Installation (Faster sync with turbosprockets)\n\nIt's possible to improve **asset:precompile** time if you are using Rails 3.2.x\nthe main source of which being compilation of **non-digest** assets.\n\n[turbo-sprockets-rails3](https://github.com/ndbroadbent/turbo-sprockets-rails3)\nsolves this by only compiling **digest** assets. Thus cutting compile time in half.\n\n\u003e NOTE: It will be **deprecated in Rails 4** as sprockets-rails has been extracted\nout of Rails and will only compile **digest** assets by default.\n\n## Configuration\n\n### Rails\n\nConfigure __config/environments/production.rb__ to use Amazon\nS3 as the asset host and ensure precompiling is enabled.\n\n\n``` ruby\n  #config/environments/production.rb\n  config.action_controller.asset_host = \"//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com\"\n```\n\nOr, to use Google Storage Cloud, configure as this.\n\n``` ruby\n  #config/environments/production.rb\n  config.action_controller.asset_host = \"//#{ENV['FOG_DIRECTORY']}.storage.googleapis.com\"\n```\n\nOr, to use Azure Blob storage, configure as this.\n\n``` ruby\n  #config/environments/production.rb\n  config.action_controller.asset_host = \"//#{ENV['AZURE_STORAGE_ACCOUNT_NAME']}.blob.core.windows.net/#{ENV['FOG_DIRECTORY']}\"\n```\n\nOr, to use Backblaze B2, configure as this.\n\n``` ruby\n  #config/environments/production.rb\n  config.action_controller.asset_host = \"//f000.backblazeb2.com/file/#{ENV['FOG_DIRECTORY']}\"\n```\n\n\nOn **HTTPS**: the exclusion of any protocol in the asset host declaration above will allow browsers to choose the transport mechanism on the fly. So if your application is available under both HTTP and HTTPS the assets will be served to match.\n\n\u003e The only caveat with this is that your S3 bucket name **must not contain any periods** so, mydomain.com.s3.amazonaws.com for example would not work under HTTPS as SSL certificates from Amazon would interpret our bucket name as **not** a subdomain of s3.amazonaws.com, but a multi level subdomain. To avoid this don't use a period in your subdomain or switch to the other style of S3 URL.\n\n``` ruby\n  config.action_controller.asset_host = \"//s3.amazonaws.com/#{ENV['FOG_DIRECTORY']}\"\n```\n\nOr, to use Google Storage Cloud, configure as this.\n\n``` ruby\n  config.action_controller.asset_host = \"//storage.googleapis.com/#{ENV['FOG_DIRECTORY']}\"\n```\n\nOr, to use Azure Blob storage, configure as this.\n\n``` ruby\n  #config/environments/production.rb\n  config.action_controller.asset_host = \"//#{ENV['AZURE_STORAGE_ACCOUNT_NAME']}.blob.core.windows.net/#{ENV['FOG_DIRECTORY']}\"\n```\n\nOn **non default S3 bucket region**: If your bucket is set to a region that is not the default US Standard (us-east-1) you must use the first style of url ``//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com`` or amazon will return a 301 permanently moved when assets are requested. Note the caveat above about bucket names and periods.\n\nIf you wish to have your assets sync to a sub-folder of your bucket instead of into the root add the following to your ``production.rb`` file\n\n``` ruby\n  # store assets in a 'folder' instead of bucket root\n  config.assets.prefix = \"/production/assets\"\n```\n\nAlso, ensure the following are defined (in production.rb or application.rb)\n\n* **config.assets.digest** is set to **true**.\n* **config.assets.enabled** is set to **true**.\n\nAdditionally, if you depend on any configuration that is setup in your `initializers` you will need to ensure that\n\n* **config.assets.initialize\\_on\\_precompile** is set to **true**\n\n### AssetSync\n\n**AssetSync** supports the following methods of configuration.\n\n* [Built-in Initializer](https://github.com/AssetSync/asset_sync/blob/master/lib/asset_sync/engine.rb) (configured through environment variables)\n* Rails Initializer\n* A YAML config file\n\n\nUsing the **Built-in Initializer** is the default method and is supposed to be used with **environment** variables. It's the recommended approach for deployments on Heroku.\n\nIf you need more control over configuration you will want to use a **custom rails initializer**.\n\nConfiguration using a **YAML** file (a common strategy for Capistrano deployments) is also supported.\n\nThe recommend way to configure **asset_sync** is by using **environment variables** however it's up to you, it will work fine if you hard code them too. The main reason why using environment variables is recommended is so your access keys are not checked into version control.\n\n\n### Built-in Initializer (Environment Variables)\n\nThe Built-in Initializer will configure **AssetSync** based on the contents of your environment variables.\n\nAdd your configuration details to **heroku**\n\n``` bash\nheroku config:add AWS_ACCESS_KEY_ID=xxxx\nheroku config:add AWS_SECRET_ACCESS_KEY=xxxx\nheroku config:add FOG_DIRECTORY=xxxx\nheroku config:add FOG_PROVIDER=AWS\n# and optionally:\nheroku config:add FOG_REGION=eu-west-1\nheroku config:add ASSET_SYNC_COMPRESSION=gz\nheroku config:add ASSET_SYNC_MANIFEST=true\nheroku config:add ASSET_SYNC_EXISTING_REMOTE_FILES=keep\n```\n\nOr add to a traditional unix system\n\n``` bash\nexport AWS_ACCESS_KEY_ID=xxxx\nexport AWS_SECRET_ACCESS_KEY=xxxx\nexport FOG_DIRECTORY=xxxx\n```\n\nRackspace configuration is also supported\n\n``` bash\nheroku config:add RACKSPACE_USERNAME=xxxx\nheroku config:add RACKSPACE_API_KEY=xxxx\nheroku config:add FOG_DIRECTORY=xxxx\nheroku config:add FOG_PROVIDER=Rackspace\n```\n\nGoogle Storage Cloud configuration is supported as well. The preferred option is using the [GCS JSON API](https://github.com/fog/fog-google#storage) which requires that you create an appropriate service account, generate the signatures and make them accessible to asset sync at the prescribed location\n\n```bash\nheroku config:add FOG_PROVIDER=Google\nheroku config:add GOOGLE_PROJECT=xxxx\nheroku config:add GOOGLE_JSON_KEY_LOCATION=xxxx\nheroku config:add FOG_DIRECTORY=xxxx\n```\n\nIf using the S3 API the following config is required\n\n``` bash\nheroku config:add FOG_PROVIDER=Google\nheroku config:add GOOGLE_STORAGE_ACCESS_KEY_ID=xxxx\nheroku config:add GOOGLE_STORAGE_SECRET_ACCESS_KEY=xxxx\nheroku config:add FOG_DIRECTORY=xxxx\n```\n\nThe Built-in Initializer also sets the AssetSync default for **existing_remote_files** to **keep**.\n\n### Custom Rails Initializer (config/initializers/asset_sync.rb)\n\nIf you want to enable some of the advanced configuration options you will want to create your own initializer.\n\nRun the included Rake task to generate a starting point.\n\n    rails g asset_sync:install --provider=Rackspace\n    rails g asset_sync:install --provider=AWS\n    rails g asset_sync:install --provider=AzureRM\n    rails g asset_sync:install --provider=Backblaze\n\nThe generator will create a Rails initializer at `config/initializers/asset_sync.rb`.\n\n``` ruby\nAssetSync.configure do |config|\n  config.fog_provider = 'AWS'\n  config.fog_directory = ENV['FOG_DIRECTORY']\n  config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']\n  config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']\n  config.aws_session_token = ENV['AWS_SESSION_TOKEN'] if ENV.key?('AWS_SESSION_TOKEN')\n\n  # Don't delete files from the store\n  # config.existing_remote_files = 'keep'\n  #\n  # Increase upload performance by configuring your region\n  # config.fog_region = 'eu-west-1'\n  #\n  # Set `public` option when uploading file depending on value,\n  # Setting to \"default\" makes asset sync skip setting the option\n  # Possible values: true, false, \"default\" (default: true)\n  # config.fog_public = true\n  #\n  # Change AWS signature version. Default is 4\n  # config.aws_signature_version = 4\n  #\n  # Change canned ACL of uploaded object. Default is unset. Will override fog_public if set.\n  # Choose from: private | public-read | public-read-write | aws-exec-read |\n  #              authenticated-read | bucket-owner-read | bucket-owner-full-control\n  # config.aws_acl = nil\n  #\n  # Change host option in fog (only if you need to)\n  # config.fog_host = 's3.amazonaws.com'\n  #\n  # Change port option in fog (only if you need to)\n  # config.fog_port = \"9000\"\n  #\n  # Use http instead of https.\n  # config.fog_scheme = 'http'\n  #\n  # Extra fog options.\n  # Overrides any existing value (even those set by AssetSync)\n  # config.fog_options = {}\n  #\n  # Automatically replace files with their equivalent gzip/brotli compressed version\n  # config.compression = 'gz'\n  #\n  # Use the Rails generated 'manifest.yml' file to produce the list of files to\n  # upload instead of searching the assets directory.\n  # config.manifest = true\n  #\n  # Upload the manifest file also.\n  # config.include_manifest = false\n  #\n  # Upload files concurrently\n  # config.concurrent_uploads = false\n  #\n  # Number of threads when concurrent_uploads is enabled\n  # config.concurrent_uploads_max_threads = 10\n  #\n  # Path to cache file to skip scanning remote\n  # config.remote_file_list_cache_file_path = './.asset_sync_remote_file_list_cache.json'\n  #\n  # Path on remote storage to persist remote file list file\n  # config.remote_file_list_remote_path = '/remote/asset_sync_remote_file.json'\n  #\n  # Fail silently.  Useful for environments such as Heroku\n  # config.fail_silently = true\n  #\n  # Log silently. Default is `true`. But you can set it to false if more logging message are preferred.\n  # Logging messages are sent to `STDOUT` when `log_silently` is falsy\n  # config.log_silently = true\n  #\n  # Allow custom assets to be cacheable. Note: The base filename will be matched\n  # If you have an asset with name `app.0b1a4cd3.js`, only `app.0b1a4cd3` will need to be matched\n  # only one of `cache_asset_regexp` or `cache_asset_regexps` is allowed.\n  # config.cache_asset_regexp = /\\.[a-f0-9]{8}$/i\n  # config.cache_asset_regexps = [ /\\.[a-f0-9]{8}$/i, /\\.[a-f0-9]{20}$/i ]\nend\n```\n\n### YAML (config/asset_sync.yml)\n\nRun the included Rake task to generate a starting point.\n\n    rails g asset_sync:install --use-yml --provider=Rackspace\n    rails g asset_sync:install --use-yml --provider=AWS\n    rails g asset_sync:install --use-yml --provider=AzureRM\n    rails g asset_sync:install --use-yml --provider=Backblaze\n\nThe generator will create a YAML file at `config/asset_sync.yml`.\n\n``` yaml\ndefaults: \u0026defaults\n  fog_provider: \"AWS\"\n  fog_directory: \"rails-app-assets\"\n  aws_access_key_id: \"\u003c%= ENV['AWS_ACCESS_KEY_ID'] %\u003e\"\n  aws_secret_access_key: \"\u003c%= ENV['AWS_SECRET_ACCESS_KEY'] %\u003e\"\n\n  # To use AWS reduced redundancy storage.\n  # aws_reduced_redundancy: true\n  #\n  # You may need to specify what region your storage bucket is in\n  # fog_region: \"eu-west-1\"\n  #\n  # Change AWS signature version. Default is 4\n  # aws_signature_version: 4\n  #\n  # Change canned ACL of uploaded object. Default is unset. Will override fog_public if set.\n  # Choose from: private | public-read | public-read-write | aws-exec-read |\n  #              authenticated-read | bucket-owner-read | bucket-owner-full-control\n  # aws_acl: null\n  #\n  # Change host option in fog (only if you need to)\n  # fog_host: \"s3.amazonaws.com\"\n  #\n  # Use http instead of https. Default should be \"https\" (at least for fog-aws)\n  # fog_scheme: \"http\"\n\n  existing_remote_files: keep # Existing pre-compiled assets on S3 will be kept\n  # To delete existing remote files.\n  # existing_remote_files: delete\n  # To ignore existing remote files and overwrite.\n  # existing_remote_files: ignore\n  # Automatically replace files with their equivalent gzip/brotli compressed version\n  # compression: \"gz\"\n  # Fail silently.  Useful for environments such as Heroku\n  # fail_silently: true\n  # Always upload. Useful if you want to overwrite specific remote assets regardless of their existence\n  #  eg: Static files in public often reference non-fingerprinted application.css\n  #  note: You will still need to expire them from the CDN's edge cache locations\n  # always_upload: ['application.js', 'application.css', !ruby/regexp '/application-/\\d{32}\\.css/']\n  # Ignored files. Useful if there are some files that are created dynamically on the server and you don't want to upload on deploy.\n  # ignored_files: ['ignore_me.js', !ruby/regexp '/ignore_some/\\d{32}\\.css/']\n  # Public path. To change the public directory.\n  # public_path: 'public'\n  # Prefix. To change the directory to sync relative to public_path.\n  # prefix: 'assets'\n  # Allow custom assets to be cacheable. Note: The base filename will be matched\n  # If you have an asset with name \"app.0b1a4cd3.js\", only \"app.0b1a4cd3\" will need to be matched\n  # cache_asset_regexps: ['cache_me.js', !ruby/regexp '/cache_some\\.\\d{8}\\.css/']\n\ndevelopment:\n  \u003c\u003c: *defaults\n\ntest:\n  \u003c\u003c: *defaults\n\nproduction:\n  \u003c\u003c: *defaults\n```\n\n### Available Configuration Options\n\nMost AssetSync configuration can be modified directly using environment variables with the **Built-in initializer**. e.g.\n\n```ruby\nAssetSync.config.fog_provider == ENV['FOG_PROVIDER']\n```\n\nSimply **upcase** the ruby attribute names to get the equivalent environment variable to set. The only exception to that rule are the internal **AssetSync** config variables, they must be prepended with `ASSET_SYNC_*` e.g.\n\n```ruby\nAssetSync.config.compression == ENV['ASSET_SYNC_COMPRESSION']\n```\n\n#### AssetSync (optional)\n\n* **existing_remote_files**: (`'keep', 'delete', 'ignore'`) what to do with previously precompiled files. **default:** `'keep'`\n* **gzip\\_compression**: (deprecated, can still be used but use `compression` when possible)\n* **compression**: (`'gz', 'br'`) when enabled (non `nil` value set), will automatically replace files that have a gzip/brotli compressed equivalent with the compressed version. **default:** `nil`\n* **manifest**: (`true, false`) when enabled, will use the `manifest.yml` generated by Rails to get the list of local files to upload. **experimental**. **default:** `'false'`\n* **include_manifest**: (`true, false`) when enabled, will upload the `manifest.yml` generated by Rails. **default:** `'false'`\n* **concurrent_uploads**: (`true, false`) when enabled, will upload the files in different Threads, this greatly improves the upload speed. **default:** `'false'`\n* **concurrent_uploads_max_threads**: when concurrent_uploads is enabled, this determines the number of threads that will be created. **default:** `10`\n* **remote_file_list_cache_file_path**: if present, use this path to cache remote file list to skip scanning remote **default:** `nil`\n* **remote_file_list_remote_path**: if present, use this path to download remote file list file to cache file list in local to skip scanning remote. useful in container environment where you cannot maintain the local file, remote_file_list_cache_file_path also needed to make use of this option **default:** `nil`\n* **enabled**: (`true, false`) when false, will disable asset sync. **default:** `'true'` (enabled)\n* **ignored\\_files**: an array of files to ignore e.g. `['ignore_me.js', %r(ignore_some/\\d{32}\\.css)]` Useful if there are some files that are created dynamically on the server and you don't want to upload on deploy **default**: `[]`\n* **cache\\_asset\\_regexps**: an array of files to add cache headers e.g. `['cache_me.js', %r(cache_some\\.\\d{8}\\.css)]` Useful if there are some files that are added to sprockets assets list and need to be set as 'Cacheable' on uploaded server.  Only rails compiled regexp is matched internally **default**: `[]`\n\n##### Config Method `add_local_file_paths`\nAdding local files by providing a block:\n```ruby\nAssetSync.configure do |config|\n  # The block should return an array of file paths\n  config.add_local_file_paths do\n    # Any code that returns paths of local asset files to be uploaded\n    # Like Webpacker\n    public_root = Rails.root.join(\"public\")\n    Dir.chdir(public_root) do\n      packs_dir = Webpacker.config.public_output_path.relative_path_from(public_root)\n      Dir[File.join(packs_dir, '/**/**')]\n    end\n  end\nend\n```\nThe blocks are run when local files are being scanned and uploaded\n\n##### Config Method `file_ext_to_mime_type_overrides`\nIt's reported that `mime-types` 3.x returns `application/ecmascript` instead of `application/javascript`  \nSuch change of mime type might cause some CDN to disable asset compression  \nSo this gem has defined a default override for file ext `js` to be mapped to `application/javascript` by default\n\nTo customize the overrides:\n```ruby\nAssetSync.configure do |config|\n  # Clear the default overrides\n  config.file_ext_to_mime_type_overrides.clear\n\n  # Add/Edit overrides\n  # Will call `#to_s` for inputs\n  config.file_ext_to_mime_type_overrides.add(:js, :\"application/x-javascript\")\nend\n```\nThe blocks are run when local files are being scanned and uploaded\n\n#### Fog (Required)\n* **fog\\_provider**: your storage provider *AWS* (S3) or *Rackspace* (Cloud Files) or *Google* (Google Storage) or *AzureRM* (Azure Blob) or *Backblaze* (Backblaze B2)\n* **fog\\_directory**: your bucket name\n\n#### Fog (Optional)\n\n* **fog\\_region**: the region your storage bucket is in e.g. *eu-west-1* (AWS),  *ord* (Rackspace), *japanwest* (Azure Blob)\n* **fog\\_path\\_style**: To use buckets with dot in names, check https://github.com/fog/fog/issues/2381#issuecomment-28088524\n* **fog\\_options**: For extra fog options. Overrides any existing value (even those set by AssetSync)\n\n#### AWS\n\n* **aws\\_access\\_key\\_id**: your Amazon S3 access key\n* **aws\\_secret\\_access\\_key**: your Amazon S3 access secret\n* **aws\\_acl**: set [canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl) of uploaded object, will override fog_public if set\n\n#### Rackspace\n\n* **rackspace\\_username**: your Rackspace username\n* **rackspace\\_api\\_key**: your Rackspace API Key.\n\n#### Google Storage\n\nWhen using the JSON API\n\n- **google\\_project**: your Google Cloud Project name where the Google Cloud Storage bucket resides\n- **google\\_json\\_key\\_location**: path to the location of the service account key.  The service account key must be a JSON type key\n\nWhen using the S3 API\n\n* **google\\_storage\\_access\\_key\\_id**: your Google Storage access key\n* **google\\_storage\\_secret\\_access\\_key**: your Google Storage access secret\n\n#### Azure Blob\n* **azure\\_storage\\_account\\_name**: your Azure Blob access key\n* **azure\\_storage\\_access\\_key**: your Azure Blob access secret\n\n#### Backblaze B2\n* **b2\\_key\\_id**: Your Backblaze B2 key ID\n* **b2\\_key\\_token**: Your Backblaze B2 key token\n* **b2\\_bucket\\_id**: Your Backblaze B2 bucket ID\n\n#### Rackspace (Optional)\n\n* **rackspace\\_auth\\_url**: Rackspace auth URL, for Rackspace London use: `https://lon.identity.api.rackspacecloud.com/v2.0`\n\n## Amazon S3 Multiple Region Support\n\nIf you are using anything other than the US buckets with S3 then you'll want to set the **region**. For example with an EU bucket you could set the following environment variable.\n\n``` bash\nheroku config:add FOG_REGION=eu-west-1\n```\n\nOr via a custom initializer\n\n``` ruby\nAssetSync.configure do |config|\n  # ...\n  config.fog_region = 'eu-west-1'\nend\n```\n\nOr via YAML\n\n``` yaml\nproduction:\n  # ...\n  fog_region: 'eu-west-1'\n```\n\n### Amazon (AWS) IAM Users\n\nAmazon has switched to the more secure IAM User security policy model. When generating a user \u0026 policy for asset_sync you **must** ensure the policy has the following permissions, or you'll see the error:\n\n```\nExpected(200) \u003c=\u003e Actual(403 Forbidden)\n```\n\nIAM User Policy Example with minimum require permissions (replace `bucket_name` with your bucket):\n\n``` json\n{\n  \"Statement\": [\n    {\n      \"Action\": \"s3:ListBucket\",\n      \"Effect\": \"Allow\",\n      \"Resource\": \"arn:aws:s3:::bucket_name\"\n    },\n    {\n      \"Action\": \"s3:PutObject*\",\n      \"Effect\": \"Allow\",\n      \"Resource\": \"arn:aws:s3:::bucket_name/*\"\n    }\n  ]\n}\n```\n\nIf you want to use IAM roles you must set ```config.aws_iam_roles = true``` in your initializers.\n\n```\nAssetSync.configure do |config|\n  # ...\n  config.aws_iam_roles = true\nend\n```\n\n\n## Automatic gzip/brotli compression\n\nWith the `compression` option enabled, when uploading your assets. If a file has a gzip/brotli (only 1 of them, depending on the option value set) compressed equivalent we will replace that asset with the compressed version and sets the correct headers for S3 to serve it. For example, if you have a file **master.css** and it was compressed to **master.css.gz** we will upload the **.gz** file to S3 in place of the uncompressed file.\n\nIf the compressed file is actually larger than the uncompressed file we will ignore this rule and upload the standard uncompressed version.\n\nOption `compression` supported values:\n- `nil` =\u003e Disabled\n- `'gz'` =\u003e Only consider gzip compressed one\n- `'br'` =\u003e Only consider brotli compressed one\n\n## Fail Silently\n\nWith the `fail_silently` option enabled, when running `rake assets:precompile` AssetSync will never throw an error due to missing configuration variables.\n\nWith the new **user_env_compile** feature of Heroku (see above), this is no longer required or recommended. Yet was added for the following reasons:\n\n\u003e With Rails 3.1 on the Heroku cedar stack, the deployment process automatically runs `rake assets:precompile`. If you are using **ENV** variable style configuration. Due to the methods with which Heroku compile slugs, there will be an error raised by asset\\_sync as the environment is not available. This causes heroku to install the `rails31_enable_runtime_asset_compilation` plugin which is not necessary when using **asset_sync** and also massively slows down the first incoming requests to your app.\n\n\u003e To prevent this part of the deploy from failing (asset_sync raising a config error), but carry on as normal set `fail_silently` to true in your configuration and ensure to run `heroku run rake assets:precompile` after deploy.\n\n## Rake Task\n\nA rake task is included within the **asset_sync** gem to perform the sync:\n\n``` ruby\n  namespace :assets do\n    desc \"Synchronize assets to S3\"\n    task :sync =\u003e :environment do\n      AssetSync.sync\n    end\n  end\n```\n\nIf `AssetSync.config.run_on_precompile` is `true` (default), then assets will be uploaded to S3 automatically after the `assets:precompile` rake task is invoked:\n\n``` ruby\n  if Rake::Task.task_defined?(\"assets:precompile:nondigest\")\n    Rake::Task[\"assets:precompile:nondigest\"].enhance do\n      Rake::Task[\"assets:sync\"].invoke if defined?(AssetSync) \u0026\u0026 AssetSync.config.run_on_precompile\n    end\n  else\n    Rake::Task[\"assets:precompile\"].enhance do\n      Rake::Task[\"assets:sync\"].invoke if defined?(AssetSync) \u0026\u0026 AssetSync.config.run_on_precompile\n    end\n  end\n```\n\nYou can disable this behavior by setting `AssetSync.config.run_on_precompile = false`.\n\n## Sinatra/Rack Support\n\nYou can use the gem with any Rack application, but you must specify two\nadditional options; `prefix` and `public_path`.\n\n```ruby\nAssetSync.configure do |config|\n  config.fog_provider = 'AWS'\n  config.fog_directory = ENV['FOG_DIRECTORY']\n  config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']\n  config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']\n  config.prefix = 'assets'\n  # Can be a `Pathname` or `String`\n  # Will be converted into an `Pathname`\n  # If relative, will be converted into an absolute path\n  # via `::Rails.root` or `::Dir.pwd`\n  config.public_path = Pathname('./public')\nend\n```\n\nThen manually call `AssetSync.sync` at the end of your asset precompilation\ntask.\n\n```ruby\nnamespace :assets do\n  desc 'Precompile assets'\n  task :precompile do\n    target = Pathname('./public/assets')\n    manifest = Sprockets::Manifest.new(sprockets, './public/assets/manifest.json')\n\n    sprockets.each_logical_path do |logical_path|\n      if (!File.extname(logical_path).in?(['.js', '.css']) || logical_path =~ /application\\.(css|js)$/) \u0026\u0026 asset = sprockets.find_asset(logical_path)\n        filename = target.join(logical_path)\n        FileUtils.mkpath(filename.dirname)\n        puts \"Write asset: #{filename}\"\n        asset.write_to(filename)\n        manifest.compile(logical_path)\n      end\n    end\n\n    AssetSync.sync\n  end\nend\n```\n\n## Webpacker (\u003e 2.0) support\n\n1. Add webpacker files and disable `run_on_precompile`:\n```ruby\nAssetSync.configure do |config|\n  # Disable automatic run on precompile in order to attach to webpacker rake task\n  config.run_on_precompile = false\n  # The block should return an array of file paths\n  config.add_local_file_paths do\n    # Support webpacker assets\n    public_root = Rails.root.join(\"public\")\n    Dir.chdir(public_root) do\n      packs_dir = Webpacker.config.public_output_path.relative_path_from(public_root)\n      Dir[File.join(packs_dir, '/**/**')]\n    end\n  end\nend\n```\n\n2. Add a `asset_sync.rake` in your `lib/tasks` directory that enhances the correct task, otherwise asset_sync runs before `webpacker:compile` does:\n```\nif defined?(AssetSync)\n  Rake::Task['webpacker:compile'].enhance do\n    Rake::Task[\"assets:sync\"].invoke\n  end\nend\n```\n\n### Caveat\nBy adding local files outside the normal Rails `assets` directory, the uploading part works, however checking that the asset was previously uploaded is not working because asset_sync is only fetching the files in the `assets` directory on the remote bucket. This will mean additional time used to upload the same assets again on every precompilation.\n\n## Running the specs\n\nMake sure you have a .env file with these details:-\n\n    # for AWS provider\n    AWS_ACCESS_KEY_ID=\u003cyourkeyid\u003e\n    AWS_SECRET_ACCESS_KEY=\u003cyoursecretkey\u003e\n    FOG_DIRECTORY=\u003cyourbucket\u003e\n    FOG_REGION=\u003cyoubucketregion\u003e\n\n    # for AzureRM provider\n    AZURE_STORAGE_ACCOUNT_NAME=\u003cyouraccountname\u003e\n    AZURE_STORAGE_ACCESS_KEY=\u003cyouraccesskey\u003e\n    FOG_DIRECTORY=\u003cyourcontainer\u003e\n    FOG_REGION=\u003cyourcontainerregion\u003e\n\nMake sure the bucket has read/write permissions.  Then to run the tests:-\n\n    foreman run rake\n\n## Todo\n\n1. Add some before and after filters for deleting and uploading\n2. Support more cloud storage providers\n3. Better test coverage\n4. Add rake tasks to clean old assets from a bucket\n\n## Credits\n\nInspired by:\n\n - [https://github.com/moocode/asset_id](https://github.com/moocode/asset_id)\n - [https://gist.github.com/1053855](https://gist.github.com/1053855)\n\n## License\n\nMIT License. Copyright 2011-2013 Rumble Labs Ltd. [rumblelabs.com](http://rumblelabs.com)\n","funding_links":[],"categories":["Uncategorized","Ruby","Assets"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAssetSync%2Fasset_sync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAssetSync%2Fasset_sync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAssetSync%2Fasset_sync/lists"}