开发者社区> 问答> 正文

C嵌套python,oss下载遇oss2.exceptions.RequestError

if (!(pFunc = PyDict_GetItemString(pDict, "Auth"))) {
        cout << "Failed to get Function Auth()";
        return false;
    }
if (!(authR = PyObject_CallFunction(pFunc, "(ss)", authID, authSec))) {
        cout << "Auth exec failed!";
        return false;
    }
if (!(pFunc = PyObject_GetAttrString(pModule, "Bucket"))) {
        cout << "Failed to get Function Bucket()";
        return false;
    }
if (!(bucketR = PyObject_CallFunction(pFunc, "Oss", authR, endpoint, bucket))) {
        cout << "Bucket exec failed!" << endl;
        return false;
    }
char* getobject = "get_object_to_file";
PyObject_CallMethod(bucketR, getobject,"(su)",filename,pathname);

主要代码就是这一部分,执行完返回
Exception oss2.exceptions.RequestError: RequestError() in <module 'threading' from 'C:\Python27\Lib\threading.pyc'> ignored


展开
收起
jdcrew 2016-10-25 15:50:33 6091 0
2 条回答
写回答
取消 提交回答
  • 回 1楼deadbeef的帖子
    你好,我现在这个代码逻辑上有错误么?
    我用C SDK和python sdk以及C调用python代码/文件都走通过了.
    我感觉按照逻辑应该是没问题的,但是为什么出现这个RequestError我也不知道为什么,,能否给点提示呢?解决这个bug

    -------------------------

    回 3楼deadbeef的帖子
    你好,我个人昨天解决了这个bug了,主要还是传参引起的错误。谢谢你的回答,你的代码也有借鉴意义,谢谢
    2016-10-26 15:54:57
    赞同 展开评论 打赏
  • RequestError表明是底层的http库(requests)返回了异常,通常是访问不通等问题导致。

    oss有C SDK,你是不是考虑直接用C SDK?或者自己先写一个python代码把完整的逻辑走通,然后用C调用你那个python代码。

    -------------------------

    我写了一段测试代码,没有问题。但是如果我把 endpoint 填错的话,就碰到和你一样的错误。

    请你检查下 endpoint 是否正确,自己可以curl一下。

    测试代码如下(注意:不是生产代码,错误处理,内存释放基本没有做):

    #include <Python.h>

    PyObject* GetCallable(PyObject* module, const char* name)
    {
        PyObject* creator = PyObject_GetAttrString(module, name);
        if (!creator) {
            printf("%s not found\n", name);
            return NULL;
        }

        if (!PyCallable_Check(creator)) {
            printf("%s is not callable\n", name);
            Py_DECREF(creator);

            return NULL;
        }

        return creator;
    }

    int
    main(int argc, char *argv[])
    {
        const char* accessKeyId = "<填写正确的值>";
        const char* accessKeySecret = "<填写正确的值>";

        const char* endpoint = "http://oss-cn-hangzhou.aliyuncs.com";   /* take hangzhou region as an example */
        const char* bucketName = "<填写正确的值>";

        Py_Initialize();

        PyObject* moduleName = PyString_FromString("oss2");
        if (!moduleName) {
            printf("bad module name\n");
            exit(1);
        }

        PyObject* module = PyImport_Import(moduleName);

        if (!module) {
            printf("module not found");
            exit(1);
        }

        PyObject* authCreator = GetCallable(module, "Auth");
        if (!authCreator) {
            exit(1);
        }

        PyObject* bucketCreator = GetCallable(module, "Bucket");
        if (!bucketCreator) {
            exit(1);
        }

        PyObject* auth = PyObject_CallFunction(authCreator, "ss", accessKeyId, accessKeySecret);
        if (!auth) {
            printf("failed to instantiate auth\b");
            exit(1);
        }

        PyObject* bucket = PyObject_CallFunction(bucketCreator, "Oss", auth, endpoint, bucketName);
        if (!bucket) {
            printf("failed to instantiate bucket\n");
            exit(1);
        }

        PyObject_CallMethod(bucket, "get_object_to_file", "ss", "test.txt", "local.txt");

        Py_Finalize();

        printf("done\n");

        return 0;
    }

    2016-10-26 09:34:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载