如何从 Heroku bash 下载文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21465789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I download a file from Heroku bash?
提问by niklasae
I ran a ruby script from Heroku bash that generates a CSV file on the server that I want to download. I tried moving it to the public folder to download, but that didn't work. I figured out that after every session in the Heroku bash console, the files delete. Is there a command to download directly from the Heroku bash console?
我从 Heroku bash 运行了一个 ruby 脚本,它在我想下载的服务器上生成一个 CSV 文件。我尝试将其移动到公共文件夹进行下载,但没有奏效。我发现在 Heroku bash 控制台中的每个会话之后,文件都会删除。是否有直接从 Heroku bash 控制台下载的命令?
采纳答案by friism
Heroku dyno filesystems are ephemeral, non-persistant and not shared between dynos. So when you do heroku run bash
, you actually get a new dyno with a fresh deployment of you app without any of the changes made to ephemeral filesystems in other dynos.
Heroku dyno 文件系统是短暂的、非持久的并且不在 dyno 之间共享。因此,当您这样做时heroku run bash
,您实际上会获得一个新的 dyno,并重新部署了您的应用程序,而无需对其他 dyno 中的临时文件系统进行任何更改。
If you want to do something like this, you should probably either do it all in a heroku run bash
session or all in a request to a web app running on Heroku that responds with the CSV file you want.
如果您想做这样的事情,您可能应该在heroku run bash
会话中完成所有操作,或者在对 Heroku 上运行的 Web 应用程序的请求中完成所有操作,该应用程序以您想要的 CSV 文件进行响应。
回答by niklasae
If you manage to create the file from heroku run bash
, you could use transfer.sh.
如果您设法从 中创建文件heroku run bash
,则可以使用transfer.sh。
You can even encrypt the file before you transfer it.
您甚至可以在传输文件之前对其进行加密。
cat <file_name> | gpg -ac -o- | curl -X PUT -T "-" https://transfer.sh/<file_name>.gpg
And then download and decrypt it on the target machine
然后在目标机器上下载解密
curl https://transfer.sh/<hash>/<file_name>.gpg | gpg -o- > <file_name>
回答by Szymon Je?
There is heroku ps:copy
:
#$ heroku help ps:copy
Copy a file from a dyno to the local filesystem
USAGE
$ heroku ps:copy FILE
OPTIONS
-a, --app=app (required) app to run command against
-d, --dyno=dyno specify the dyno to connect to
-o, --output=output the name of the output file
-r, --remote=remote git remote of app to use
DESCRIPTION
Example:
$ heroku ps:copy FILENAME --app murmuring-headland-14719
Example run:
示例运行:
#$ heroku ps:copy app.json --app=app-example-prod --output=app.json.from-heroku
Copying app.json to app.json.from-heroku
Establishing credentials... done
Connecting to web.1 on ? app-example-prod...
Downloading... ████████████████████████▏ 100% 00:00
Caveat
警告
This seems not to run with dynos that are run
via heroku run
.
这似乎不适用于run
通过heroku run
.
Example
例子
#$ heroku ps:copy tmp/some.log --app app-example-prod --dyno run.6039 --output=tmp/some.heroku.log
Copying tmp/some.log to tmp/some.heroku.log
Establishing credentials... error
? Could not connect to dyno!
? Check if the dyno is running with `heroku ps'
It is! Prove:
这是!证明:
#$ heroku ps --app app-example-prod
=== run: one-off processes (1)
run.6039 (Standard-1X): up 2019/08/29 12:09:13 +0200 (~ 16m ago): bash
=== web (Standard-2X): elixir --sname dyno -S mix phx.server --no-compile (2)
web.1: up 2019/08/29 10:41:35 +0200 (~ 1h ago)
web.2: up 2019/08/29 10:41:39 +0200 (~ 1h ago)
I could connect to web.1
though:
我可以连接到web.1
:
#$ heroku ps:copy tmp/some.log --app app-example-prod --dyno web.1 --output=tmp/some.heroku.log
Copying tmp/some.log to tmp/some.heroku.log
Establishing credentials... done
Connecting to web.1 on ? app-example-prod...
? ERROR: Could not transfer the file!
? Make sure the filename is correct.
So I fallen back to using SCP scp -P PORT tmp/some.log user@host:/path/some.heroku.log
from the run.6039
dyno command line.
所以我退回到scp -P PORT tmp/some.log user@host:/path/some.heroku.log
从run.6039
dyno 命令行使用 SCP 。
回答by Pouya Gharib Pour
I did as the following:
我做了如下:
First I entered heroku bash with this command:
heroku run 'sh'
Then made a directory and moved the file to that
- Made a git repository and commited the file
- Finally I pushed this repository to github
首先,我使用以下命令进入 heroku bash:
heroku 运行 'sh'
然后创建一个目录并将文件移动到该目录
- 制作了一个 git 存储库并提交了文件
- 最后我把这个仓库推送到了 github
Before commiting, git will ask you for your name and email. Give it something fake!
在提交之前,git 会询问您的姓名和电子邮件。给它一些假的!
If you have files bigger than 100 Mg, push to gitlab.
如果您的文件大于 100 Mg,请推送到 gitlab。
If there is an easier way please let me know!
如果有更简单的方法请告诉我!
Sorry for my bad english.
对不起,我的英语不好。
回答by Christos K
I agree that most probably your need means a change in your application architecture, something like a worker dyno.
But by executing the following steps you can transfer the file, since heroku one-off dyno can run scp
:
我同意您的需求很可能意味着您的应用程序架构发生变化,例如工作人员 dyno。但是通过执行以下步骤,您可以传输文件,因为 heroku 一次性 dyno 可以运行scp
:
- create vm in a cloud provider, e.g. digital ocean;
- run heroku one-off dyno and create your file;
scp
file from heroku one-off dyno to that vm server;scp
file from vm server to your local machine;- delete cloud vm and stop heroku one-off dyno.
- 在云提供商中创建虚拟机,例如数字海洋;
- 运行 heroku one-off dyno 并创建您的文件;
scp
从 heroku 一次性 dyno 到那个 vm 服务器的文件;scp
文件从虚拟机服务器到您的本地机器;- 删除云虚拟机并停止 heroku 一次性 dyno。
回答by iainbeeston
Another way of doing this (that doesn't involve any third server) is to use Patrick's method but first compress the file into a format that only uses visible ASCII charaters. That should make it work for any file, regardless of any whitespace characters or unusual encodings. I'd recommend base64
to do this.
执行此操作的另一种方法(不涉及任何第三方服务器)是使用 Patrick 的方法,但首先将文件压缩为仅使用可见 ASCII 字符的格式。这应该使它适用于任何文件,而不管任何空白字符或不寻常的编码。我建议base64
这样做。
Here's how I've done it:
这是我如何做到的:
- Log onto your heroku instance using
heroku run bash
- Use base64 to print the contents of your file:
base64 <your-file>
- Select the base64 text in your terminal and copy it
- On your local machine decompress this text using
base64
straight into a new file (on a mac I'd dopbpaste | base64 --decode -o <your-file>
)
- 使用登录到您的 heroku 实例
heroku run bash
- 使用 base64 打印文件的内容:
base64 <your-file>
- 在终端中选择 base64 文本并复制它
- 在您的本地机器上,使用
base64
直接解压缩此文本到一个新文件中(在 mac 上我会这样做pbpaste | base64 --decode -o <your-file>
)
回答by Patrick
For small/quick transfers that fit comfortably in the clipboard:
对于可轻松放入剪贴板的小型/快速传输:
- Open a terminal on your local device
- Run
heroku run bash
- (Inside your remote connection, on the dyno) Run
cat filename
- Select the lines in your local terminal and copy them to your clipboard.
- Check to ensure proper newlines when pasting them.
- 在本地设备上打开终端
- 跑
heroku run bash
- (在您的远程连接中,在 dyno 上)运行
cat filename
- 在本地终端中选择行并将它们复制到剪贴板。
- 粘贴时检查以确保正确的换行符。
回答by joepin
I see that these answers are much older, so I'm assuming this is a new feature. For all those like me who are looking for an easier solution than the excellent answers already here, Heroku now has the capability to copy files quite easily with the following command: heroku ps:copy <filename>
我看到这些答案要旧得多,所以我假设这是一个新功能。对于所有像我一样正在寻找比这里已有的优秀答案更简单的解决方案的人,Heroku 现在可以使用以下命令轻松复制文件:heroku ps:copy <filename>
Note that this works with relative paths, as you'd expect. (Tested on a heroku-18 stack, downloading files at "path/to/file.ext"
请注意,如您所料,这适用于相对路径。(在 heroku-18 堆栈上测试,在“path/to/file.ext”下载文件
For reference: Heroku docs
供参考:Heroku 文档
回答by Дмитрий Чева
Now i created shell script to upload some files from to git backup repo (for example, my app.db sqlite file is gitignored and every deploy kills it)
现在我创建了 shell 脚本来上传一些文件到 git 备份存储库(例如,我的 app.db sqlite 文件被 gitignored 和每次部署杀死它)
## upload dyno files to git via SSH session
## https://devcenter.heroku.com/changelog-items/1112
# heroku ps:exec
git config --global user.email '[email protected]'
git config --global user.name 'Dmitry Cheva'
rm -rf ./.gitignore
git init
## add each file separately (-f to add git ignored files)
git add app.db -f
git commit -m "backup on `date +'%Y-%m-%d %H:%M:%S'`"
git remote add origin https://bitbucket.org/cheva/appbackup.git
git push -u origin master -f
The git will reboot after the deploy and does not store the environment, you need to perform the first 3 commands. Then you need to add files (-f for ignored ones) and push into repo (-f, because the git will require pull)
git部署后会重启,不存储环境,需要执行前3条命令。然后你需要添加文件(-f 表示被忽略的文件)并推送到 repo(-f,因为 git 需要 pull)
回答by Ira Herman
Heroku dyno's come with sftppre-installed. I tried git but was too many steps (had to generate a new ssh cert and add it to github every time), so now I am using sftp and it works great.
Heroku dyno预装了sftp。我尝试了 git 但步骤太多(每次都必须生成一个新的 ssh 证书并将其添加到 github),所以现在我使用 sftp 并且效果很好。
You'll need to have another host (like dreamhost, hostgator, godaddy, etc) - but if you do, you can:
您需要拥有另一台主机(如 Dreamhost、hostgator、godaddy 等) - 但如果您这样做,您可以:
sftp [email protected]
Accept the server fingerprint/hash, then enter your password.
接受服务器指纹/哈希,然后输入您的密码。
Once on the server, navigate to the folder you want to upload to (using cd and ls commands).
在服务器上,导航到要上传到的文件夹(使用 cd 和 ls 命令)。
Then use the command put filename.csv
and it will upload it to your web host.
然后使用该命令put filename.csv
,它会将其上传到您的网络主机。
To retrieve your file: Use an ftp client like filezilla or hit the url if you uploaded to a folder in the www or website folder path.
检索您的文件:如果您上传到 www 或网站文件夹路径中的文件夹,请使用 filezilla 等 ftp 客户端或点击 url。
This is great because it also works with multiple files and binaries as well as text files.
这很棒,因为它也适用于多个文件和二进制文件以及文本文件。