从本地计算机上的ubuntu(AWS EC2)中读取文件? [英] reading in a file from ubuntu (AWS EC2) on local machine?

查看:185
本文介绍了从本地计算机上的ubuntu(AWS EC2)中读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在AWS(Ubuntu的EC2实例)上运行的python脚本.该python脚本每天将JSON文件输出到/home/ubuntu中的目录:

I have a python script which I'm running on AWS (EC2 instance with Ubuntu). This python script outputs a JSON file daily, to a directory in /home/ubuntu:

with open("/home/ubuntu/bandsintown/sf_events.json", "w") as writeJSON:
file_str = json.dumps(allEvents, sort_keys=True)
file_str = "var sf_events = " + file_str

所有操作均符合此处的预期.我的问题是我不确定如何将此JSON(存在于ubuntu上)读入我在本地计算机上运行的javascript文件中.

All works as expected here. My issue is that I'm unsure of how to read this JSON (existing on ubuntu) into a javascript file that I'm running on my local machine.

如果我从ubuntu调用文件,则Javascript无法找到该文件:

Javascript can't find the file if I call the file from ubuntu:

<script src="/home/ubuntu/bandsintown/sf_events.json"></script>

换句话说,我想将我在云中创建的JSON读取到本地计算机上存在的文件中.我应该在home/ubuntu之外的其他地方输出JSON吗?或者,我的本地文件可以以某种方式将/home/ubuntu识别为文件位置吗?

In other words, I'd like to read in the JSON that I've created in the cloud, to a file that exists on my local machine. Should I output the JSON somewhere other than home/ubuntu? Or, can my local file somehow recognize /home/ubuntu as a file location?

先谢谢了.

推荐答案

之所以会出现此问题,是因为文件不存在在您的本地计算机上(仅在正在运行的EC2实例上). 一种可能的解决方案是将JSON文件从EC2实例上传到S3,然后将JSON文件下载到本地计算机/home/ubuntu/bandsintown/sf_events.json.

The problem occurs because the file does not exist on your local machine, only on the running EC2 instance. A possible solution is to upload the JSON file from EC2 instance to S3 and afterward download the JSON file to your local machine /home/ubuntu/bandsintown/sf_events.json.

首先,在正在运行的EC2实例上安装AWS CLI工具包 AWS CLI ,并在终端上运行以下命令

First, install the AWS CLI toolkit on running EC2 instance AWS CLI and run the following commands in the terminal

aws configure
aws s3 cp /home/ubuntu/bandsintown/sf_events.json s3://mybucket/sf_events.json

或安装Python AWS开发工具包 boto3 并通过python上传

Or install Python AWS SDK boto3 and upload it via python

s3 = boto3.resource('s3')

def upload_file_to_s3(s3_path, local_path):
    bucket = s3_path.split('/')[2] #bucket is always second as paths are S3://bucket/.././
    file_path = '/'.join(s3_path.split('/')[3:])
    response = s3.Object(bucket, file_path).upload_file(local_path)
    return response

s3_path = "s3://mybucket/sf_events.json"
local_path = "/home/ubuntu/bandsintown/sf_events.json"
upload_file_to_s3(s3_path, local_path)

然后在本地计算机上通过AWS CLI从s3下载文件

Then on your local machine download file from s3 via AWS CLI

aws configure
aws s3 cp s3://mybucket/sf_events.json /home/ubuntu/bandsintown/sf_events.json

或者,如果您更喜欢python SDK:

Or if you prefer python SDK:

s3 = boto3.resource('s3')

def download_file_from_s3(s3_path, local_path):
    bucket = s3_path.split('/')[2] #bucket is always second as paths are S3://bucket/.././
    file_path = '/'.join(s3_path.split('/')[3:])
    filename = os.path.basename(s3_path) 
    s3.Object(bucket, file_path).download_file(local_file_path)

s3_path = "s3://mybucket/sf_events.json"
local_path = "/home/ubuntu/bandsintown/sf_events.json"
download_file_from_s3(s3_path, local_path)

或使用在浏览器中运行的Javascript SDK,但我不建议您这样做,因为您必须公开存储桶并同时注意浏览器的兼容性

Or using Javascript SDK running inside of browser, but I would not recommend this because you must make your bucket public and also take care of browser compatibility issue

这篇关于从本地计算机上的ubuntu(AWS EC2)中读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆